-1

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.

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • 1
    Why re-ask the same question twice? Why not clarify your previous question? – Hovercraft Full Of Eels Feb 14 '13 at 03:33
  • 1
    Because I asked it wrongly. And (in my experience), when I ask a question wrongly and then correct it, I don't get a response, at least not for a while. Besides, this looks so much better than that bunch of text from earlier. And I did erase the previous question... – Karlovsky120 Feb 14 '13 at 03:35
  • `Object#wait` and `Object#notify` – MadProgrammer Feb 14 '13 at 03:37
  • @MadProgrammer: better to use one of the tools from the concurrent library I think. – Hovercraft Full Of Eels Feb 14 '13 at 03:37
  • What about a semaphore? – Eng.Fouad Feb 14 '13 at 03:38
  • @HovercraftFullOfEels: Yes, but for learning concurrency it might be better to start with [wait and notify](http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html). The concurrency classes hide some of the magic that is important to understand. – Russell Zahniser Feb 14 '13 at 03:39
  • Try taking a look at [Guarded Blocks](http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html), [Lock Objects](http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html) and [Concurreny](http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html) in general – MadProgrammer Feb 14 '13 at 03:40
  • @HovercraftFullOfEels I certainly wouldn't disagree, but baby steps might be required here :P – MadProgrammer Feb 14 '13 at 03:42
  • If `doSomething` in `doInBackground` changes the state of the view you are doing something wrong (as you were already told in earlier questions, afair ;). And same as advised in earlier questions: come up with a SSCCE to show what exactly you are doing and where exactly the problem is. – kleopatra Feb 16 '13 at 09:47

1 Answers1

2

You can use one of the tools from the concurrent library such as a CountDownLatch.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373