How to make a program to "sleep" until my Thread ends
I have a problem with a program that I try to make. Inside a JFrame class, I start one Thread. That thread checks something and return a boolean as true or false. Inside the JFrame class I want to check that boolean, and do actions according to the results of the thread.
Now my question is how can I make my JFrame to wait untill the Thread ends?
If you need some code, please ask, but I hope you understand where my problem is.
Re: How to make a program to "sleep" until my Thread ends
Re: How to make a program to "sleep" until my Thread ends
Quote:
Originally Posted by
helloworld922
No, no, don't use Thread.join() in a Swing application on the event thread as this risks tying up the Swing event thread putting your entire GUI to sleep and making it completely unresponsive. There are better ways to solve this including using a PropertyChangeListener and listen for the one thread to complete. A SwingWorker is great for this since it comes automatically bundled with PropertyChangeSupport.
So create a SwingWorker to do your background threading, and listen to the worker by adding a PropertyChangeListener, and resume what you're doing when its "state" property returns the SwingWorker.StateValue.DONE.
Links:
Re: How to make a program to "sleep" until my Thread ends
Quote:
Originally Posted by
Fubarable
No, no, don't use Thread.join() in a Swing application on the event thread as this risks tying up the Swing event thread putting your entire GUI to sleep and making it completely unresponsive. There are better ways to solve this including using a PropertyChangeListener and listen for the one thread to complete. A SwingWorker is great for this since it comes automatically bundled with PropertyChangeSupport.
So create a SwingWorker to do your background threading, and listen to the worker by adding a PropertyChangeListener, and resume what you're doing when its "state" property returns the SwingWorker.StateValue.DONE.
Links:
fair point. I would concur that this is the best route to follow, even if it does requires some re-write of other code.
Re: How to make a program to "sleep" until my Thread ends
It's better to use event publisher/subscriber like Fubarable suggested.
Re: How to make a program to "sleep" until my Thread ends
If u dont wana updgrade to jdk1.6 ,using thread.join() is recommended..
I am spawning some threads in my program to perform db related tasks..
and in order to make my program wait till those threads dies.. i created threads in thread array,started all threads and then iterated the array to call thread.join
Re: How to make a program to "sleep" until my Thread ends
Quote:
Originally Posted by
joshiparag123456
If u dont wana updgrade to jdk1.6 ,using thread.join() is recommended..
Read post #3, which is the correct advice in this situation (and even better, uses proper grammar - something recommended when posting to a technical forum). It leaves out explicitly mentioning SwingUtilities, which can be used prior to the introduction of SwingWorker (1.6)