user898465
user898465

Reputation: 944

How to stop thread returning before Join() is called

This is purely a theoretical question as I'm not sure the conditions to cause this issue would be common.

Say for example you have a thread that you kick off with it's start method:

Thread c = new Thread();
c.start();

Then directly after, you call the Join() method on the thread to tell the method you are in to wait until the thread had been executed to continue.

c.join();

Isn't it a possibility that the thread could possibly be executed and finish before the join method is called, therefore leaving the method unaware that it had to wait for c to finish before it continued? I suppose you could try calling the join() method before you call the start() method, yet whenever I've tried this in test cases, there is an error.

Anyone know a possible fix for this, or does the JVM handle it? As I said I haven't been able to trigger this sort of situation, but theoretically it is possible...

Upvotes: 8

Views: 5369

Answers (6)

yadab
yadab

Reputation: 2143

why don't you use CountdownLatch with callable and future. For example, http://sycure.wordpress.com/2012/01/21/example-using-java-future-and-countdownlatch/

Upvotes: 0

cdc
cdc

Reputation: 2571

You can call join() before a Thread is started, but because the Thread hasn't been started join() will return immediately.

If join() is called after the Thread is started, and the Thread has terminated, join() will also return immediately.

The only time join() will block is while the Thread.isAlive()

This code works OK...

   public static void main( String[] args ) throws InterruptedException
   {
      Thread t = new Thread( new Runnable()
      { 
         public void run()
         {
            System.out.println( "thread" );
         }
      } );
      t.join();
      System.out.println( "joined" );

      Thread.sleep( 2000 );
      System.out.println( "call start" );
      t.start(); 
   }

Upvotes: 3

Dave
Dave

Reputation: 266

join() will return immediately if the thread is not running.

Upvotes: 0

Jon Newmuis
Jon Newmuis

Reputation: 26520

c.join() means that the currently executing thread should wait for c to complete before finishing. If c executes completely before the call to c.join() is made, then that condition is satisfied, as c has completed execution. The method in which you're calling c.join() wouldn't "know about it"; c.join() would just be like a no-op.

Upvotes: 8

jlemos
jlemos

Reputation: 536

The join method semantics mean "to wait until the joining thread is finished", and that is pretty much the standard in every programming language that supports multi-threading. Therefore, it is perfectly possible to join an already finished thread, the JVM will simply detect that it has already finished and proceed with the code execution.

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32296

According to the Thread#join(long millis) source code, the isAlive() method is used to check the thread state.

/**
 * Tests if this thread is alive. A thread is alive if it has
 * been started and has not yet died.
 *
 * @return  <code>true</code> if this thread is alive;
 *          <code>false</code> otherwise.
 */
public final native boolean isAlive();

This method obviously returns false if the thread has finished, so thread.join() will immediately exit.

Upvotes: 11

Related Questions