Skip to main content

How to make a Java thread wait for another thread"s output?



I'm making a Java application with an application-logic-thread and a database-access-thread. Both of them persist for the entire lifetime of the application. However, I need to make sure that the app thread waits until the db thread is ready (currently determined by calling dbthread.isReady() ). I wouldn't mind if app thread blocked until the db thread was ready.





Thread.join() doesn't look like a solution - the db thread only exits at app shutdown.





while (!dbthread.isReady()) {} kind of works, but the empty loop consumes a lot of processor cycles.





Any other ideas? Thanks.


Comments

  1. I would really recommend that you go through a tutorial like Sun's Java Concurrency before you commence in the magical world of multithreading.

    There are also a number of good books out (google for "Concurrent Programming in Java", "Java Concurrency in Practice".

    To get to your answer:

    In your code that must wait for the dbThread, you must have something like this:

    //do some work
    synchronized(objectYouNeedToLockOn){
    while (!dbThread.isReady()){
    objectYouNeedToLockOn.wait();
    }
    }
    //continue with work after dbThread is ready


    In your dbThread's method, you would need to do something like this:

    //do db work
    synchronized(objectYouNeedToLockOn){
    //set ready flag to true (so isReady returns true)
    ready = true;
    objectYouNeedToLockOn.notifyAll();
    }
    //end thread run method here


    The objectYouNeedToLockOn I'm using in these examples are preferable the object that you need to have exclusive access to in each thread, or you could create an Object for that purpose (I would not recommend making the methods synchronized):

    private final Object lock = new Object();
    //now use lock in your synchronized blocks


    To further your understanding:
    There are other (sometimes better) ways to do the above, e.g. with CountdownLatches, etc. Since Java 5 there area a lot of nifty concurrency classes in the java.util.concurrent package and sub-packages. You really need to find material online to get to know concurrency, or get a good book.

    ReplyDelete
  2. Use a CountdownLatch with a counter of 1.

    CountdownLatch latch = new CountdownLatch(1);


    Now in the app thread do-

    latch.await();


    In the db thread, after you are done, do -

    latch.countDown();

    ReplyDelete
  3. Try the CountDownLatch class out of the java.util.concurrent package, which provides higher level synchronization mechanisms, that are far less error prone than any of the low level stuff.

    ReplyDelete
  4. You could do it using an Exchanger object shared between the two threads:

    private Exchanger<String> myDataExchanger = new Exchanger<String>();

    // Wait for thread's output
    String data;
    try {
    data = myDataExchanger.exchange("");
    } catch (InterruptedException e1) {
    // Handle Exceptions
    }


    And in the second thread:

    try {
    myDataExchanger.exchange(data)
    } catch (InterruptedException e) {

    }


    As others have said, do not take this light-hearted and just copy-paste code. Do some reading first.

    ReplyDelete
  5. public class ThreadEvent {

    private final Object lock = new Object();

    public void signal() {
    synchronized (lock) {
    lock.notify();
    }
    }

    public void await() throws InterruptedException {
    synchronized (lock) {
    lock.wait();
    }
    }
    }


    Use this class like this then:

    Create a ThreadEvent:

    ThreadEvent resultsReady = new ThreadEvent();


    In the method this is waiting for results:

    resultsReady.await();


    And in the method that is creating the results after all the results have been created:

    resultsReady.signal();


    EDIT:

    (Sorry for editing this post, but this code has a very bad race condition and I don't have enough reputation to comment)

    You can only use this if you are 100% sure that signal() is called after await(). This is the one big reason why you cannot use Java object like e.g. Windows Events.

    The if the code runs in this order:

    Thread 1: resultsReady.signal();
    Thread 2: resultsReady.await();


    then thread 2 will wait forever. This is because Object.notify() only wakes up one of the currently running threads. A thread waiting later is not awoken. This is very different from how I expect events to work, where an event is signalled until a) waited for or b) explicitly reset.

    Note: Most of the time, you should use notifyAll(), but this is not relevant to the "wait forever" problem above.

    ReplyDelete
  6. The Future interface from the java.lang.concurrent package is designed to provide access to results calculated in another thread.

    Take a look at FutureTask and ExecutorService for a ready-made way of doing this kind of thing.

    I'd strongly recommend reading Java Concurrency In Practice to anyone interested in concurrency and multithreading. It obviously concentrates on Java, but there is plenty of meat for anybody working in other languages too.

    ReplyDelete
  7. This applies to all languages:

    You want to have an event/listener model. You create a listener to wait for a particular event. The event would be created (or signaled) in your worker thread. This will block the thread until the signal is received instead of constantly polling to see if a condition is met, like the solution you currently have.

    Your situation is one of the most common causes for deadlocks- make sure you signal the other thread regardless of errors that may have occurred. Example- if your application throws an exception- and never calls the method to signal the other that things have completed. This will make it so the other thread never 'wakes up'.

    I suggest that you look into the concepts of using events and event handlers to better understand this paradigm before implementing your case.

    Alternatively you can use a blocking function call using a mutex- which will cause the thread to wait for the resource to be free. To do this you need good thread synchronization- such as:

    Thread-A Locks lock-a
    Run thread-B
    Thread-B waits for lock-a
    Thread-A unlocks lock-a (causing Thread-B to continue)
    Thread-A waits for lock-b
    Thread-B completes and unlocks lock-b

    ReplyDelete
  8. Requirement ::

    1. To wait execution of next thread until previous finished.
    2. Next thread must not start until previous thread stops, irespective of time consumption.
    3. It must be simple and easy to use.


    Answer ::

    @See java.util.concurrent.Future.get() doc.

    future.get() Waits if necessary for the computation to complete, and then retrieves its result.


    Job Done!!
    See below example

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    import org.junit.Test;

    public class ThreadTest {

    public void print(String m) {
    System.out.println(m);
    }

    public class One implements Callable<Integer> {
    public Integer call() throws Exception {
    print("One...");
    Thread.sleep(6000);
    print("One!!");
    return 100;
    }
    }

    public class Two implements Callable<String> {
    public String call() throws Exception {
    print("Two...");
    Thread.sleep(1000);
    print("Two!!");
    return "Done";
    }
    }

    public class Three implements Callable<Boolean> {
    public Boolean call() throws Exception {
    print("Three...");
    Thread.sleep(2000);
    print("Three!!");
    return true;
    }
    }

    /**
    * @See java.util.concurrent.Future.get() doc
    *
    * <p>
    * Waits if necessary for the computation to complete, and then
    * retrieves its result.
    */
    @Test
    public void poolRun() throws InterruptedException, ExecutionException {
    int n = 3;

    // Build a fixed number of thread pool
    ExecutorService pool = Executors.newFixedThreadPool(n);


    //Wait until One finishes it's task.
    pool.submit(new One()).get();

    //Wait until Two finishes it's task.
    pool.submit(new Two()).get();

    //Wait until Three finishes it's task.
    pool.submit(new Three()).get();

    pool.shutdown();
    }

    }


    Output of this program ::

    One...
    One!!
    Two...
    Two!!
    Three...
    Three!!


    You can see that takes 6sec before finishing it's task which is greater than other thread. So Future.get() waits unitl the task is done.

    If you don't use future.get() it doesn't wait to finish and executes based time consumption.

    Good Luck with Java concurrency.

    ReplyDelete
  9. If you want something quick and dirty, you can just add a Thread.sleep() call within your while loop. If the database library is something you can't change, then there is really no other easy solution. Polling the database until is ready with a wait period won't kill the performance.

    while (!dbthread.isReady()) {
    Thread.sleep(250);
    }


    Hardly something that you could call elegant code, but gets the work done.

    In case you can modify the database code, then using a mutex as proposed in other answers is better.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex