0

I have two threads a and b. a is a UI thread and b is a non UI thread.

My code looks something like this :


   start a :

   int i;

 start b (asynchronously){
      populateInt(i);
 }

//back to a
if (i = something){
     doSomething();

}

Problem with running the threads asynchronously : the populateInt() method is still running on thread b and my thread a is trying to execute doSomething() which gives faulty results.

Problem with running the threads synchronously : The Java application generates some code on the fly and the user would want to debug that code. During debug the method populateInt() takes the control to that code and the UI thread is suspended which causes the debug to hang.

How do I run the threads asynchronously while ensuring thread a executes doSomething() after thread b is done populating 'i' ?

  • You could use a [BlockingQueue](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html) to communicate between the threads. – OldCurmudgeon Sep 02 '14 at 09:14
  • 1
    You do it best [by not reinventing the wheel](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html). – Holger Sep 02 '14 at 09:27
  • Background Threads are the way to go, this could be an easier example from the one suggested by Holger: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html – BatScream Sep 02 '14 at 09:46

0 Answers0