Results 1 to 3 of 3
  1. #1
    jitheshmenon is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default Thread wait for another thread

    Hi All,

    How to make a thread wait for another thread to finish execution and then continue?

    Suppose I have a main class which does some thing in the Main thread. I start a timer from it. After I start the timer, I want the Main thread to wait for the timer thread to finish and then continue.

    I tried to use sleep on the main timer using same time used in timer. But it does not work properly.

    Any help is much appreciated.

    Thanks
    jith

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jitheshmenon View Post
    Hi All,

    How to make a thread wait for another thread to finish execution and then continue?

    Suppose I have a main class which does some thing in the Main thread. I start a timer from it. After I start the timer, I want the Main thread to wait for the timer thread to finish and then continue.

    I tried to use sleep on the main timer using same time used in timer. But it does not work properly.
    You can do it all yourself by having both threads synchronize on the same object; the main thread waits for a certain condition and the other thread (when it's finished its job) notifies any waiting thread (here: the single main thread). The main thread does the syncing and waiting and the other thread syncs and notifies when it's ready.

    Nowadays there's the java.util.concurrent package that has several fine classes available for you, so my advice is to read its API documentation.

    kind regards,

    Jos

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    supposing t is your time thread instance you could use

    t.start();
    t.join();

    inside the method main. the t.join() blocks the current thread (main) from becoming runnable until after the thread referenced by t is no longer alive.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 03:13 AM
  3. Thread Wait
    By jiexx in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 06:26 PM
  4. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •