• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to wait on a Thread without blocking EDT?  RSS feed

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a program with a GUI where I choose multiple files for copying/extracting. The code is as follows:


What I want to do is to start one thread after the previous one is finished. But this method starts all the threads at once.
So I tried with T.join() and T.activeCount().

T.activeCount() doesnt actually give the real number of threads I have started. But T.join() does exactly what I want, with the drawback of it blocking the EDT. That is I do not get any update on my JFrame until the thread is over.

Is there any workaround for this?
I want to deply one thread after the other and I also want the JFrame updting while the thread is alive.

Thank you.
 
Norm Radder
Rancher
Posts: 1733
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the SwingUtilites invokeLater method to start another thread to do the work and immediately return from the GUI called code.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norm Radder:
You could use the SwingUtilites invokeLater method to start another thread to do the work and immediately return from the GUI called code.


Thank you Norm.

Actully this works fine. I start the thread and it immediately comes back to the GUI and it updates the thread progress.
But the problem is it starts 'all' the threads at once.
I was wondering if I can start a thread, wait till it is over, and start the next thread. I can do this with the T.join() method, but when I run that, it block the EDT and the GUI is not updated until the current thread is dead.

Any workaround to have both?

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Do the loop with the join in another thread. I.e., this event handler creates one thread, which then runs this loop to create all the other threads. If you want the loop to wait for each thread to complete before starting the next, then use join(); since it's a separate thread, it won't affect the event thread.

But why have this second thread start other threads one at a time, only to wait for each one to complete before starting another? Why not just have this single second thread do all the processing itself?
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Yes. Do the loop with the join in another thread. I.e., this event handler creates one thread, which then runs this loop to create all the other threads. If you want the loop to wait for each thread to complete before starting the next, then use join(); since it's a separate thread, it won't affect the event thread.

But why have this second thread start other threads one at a time, only to wait for each one to complete before starting another? Why not just have this single second thread do all the processing itself?



Thanks Ernest.

P.S: Got what you said. Do the whole for loop in a different thread. Gotcha. Thanks.

I didnt totally get what you mean.

I only have one thread that actually does the file copying process. I did this so that the EDT can update the GUI.
When I have T.join(), it actually waits till the thread is done to start the next thread, but then the EDT is blocked and it does not update the GUI until the current thread is over.

Actually this is not a big problem, but I can't figure out if all the threads are finished processing. If I can get them to start one after the other, then maybe I can track the end of all the threads.

Thank you.
[ July 30, 2008: Message edited by: Maduranga Liyanage ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!