Is there a way I can make the SwingWorker (in doInBackground method) come to a certain point of the code, stop there, monitor a ceratin variable, and then resume when the variable changes to a acceptable value?
I know I can do this:
do {
Thread.sleep(500);
} while (variable == false);
But that is a very ugly way of doing it...
Any suggestions?
EDIT: So wait and notify it is then... However, I have absolutley no idea how to implement it, even after reading several tutorials...
So here's my current situation:
I have a SwingWorker that does something in doInBackground method:
doInBackground()
{
doSomething();
//BLOCK (PAUSE) HERE
doSomethingElse();
}
On the other side, I have an object which has a listener attached, which is triggered when doSomething(); is executed.
doSomethingElse() accesses and changes similar things as the listener, therefore, they interfere with each other.
So my goal is to add a line of code in the listener which will lift the block off of the doInBackground() code. Here's what the class with listener looks like:
public class myClass extends JPanel()
{
addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent arg0)
{
doThings();
//LIFT BLOCK AND LET THE SWINGWORKER CODE CONTINUE
}
}
}
So, order of operations should be:
doSomething();
doThings();
doSomethingElse();
Also, I can access the listener from the SwingWorker, but not other way around (but I might be able to make it work);
And one last thing: Listener is not invoked only by the SwingWorker, it can be invoked from other sources (e.g. user), when SwingWorker does not even have to be instantialized.