Thread - Join method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Thread - Join method

  1. #1
    Join Date
    Jul 2005
    Posts
    20

    Thread - Join method

    When we run a java program, JVM will create the main thread in which the main function will be called. Inside the main thread we can have child thread. Now for the main thread to terminate the child thread must first be dead. To know if the child thread is alive or not we can do the following.

    t1.join();
    t1.isAlive() // returns a boolean value

    Im assuming t1 as a thread object. Now how does the join() method play a role in finding out the status of the child thread. Also how does it tell the main thread that the child thread is all dead?. Besides that without the syntax t1.join(), the t1.isAlive() statement will result in an error. So what does the join() method do to the t1 thread object?.
    Last edited by solomon_13000; 06-18-2009 at 07:10 AM.

  2. #2
    Join Date
    Jul 2005
    Posts
    20
    It causes the main thread to wait for the child thread to complete processing before the main thread terminates.

  3. #3
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Calling join function of a thread causes the caller thread (Main thread in this case) to wait for the called thread (t1) to complete. In other words, t1.join() will pause Main thread till t1 completes.

    isAlive() is a method in thread to check whether t1 (called thread) is running or not. So, from Main, if you call it after t1.join(), it should always return false (because join wait till t1 dies). Calling t1.isAlive should NOT throw an error unless t1 is uninitialized. Try the code below:

    Code:
    public class ThreadTest {
    
    	public static void main(String[] args) throws Exception
    	{
    		boolean state;
    		MyThread t1 = new MyThread();
    		state = t1.isAlive(); // No error, state=false, because thread not started yet
    		System.out.println("Before start() T1::isAlive = " + state);
    		t1.start();
    		state = t1.isAlive(); // No error, state=true here, but maybe false under some circumstances depending on wether t1 is still has terminated or not
    		System.out.println("Just after start() T1::isAlive = " + state);
    		t1.join();
    		state = t1.isAlive(); // Always false
    		System.out.println("After join() T1::isAlive = " + state);
    	}
    	
    	static class MyThread extends Thread
    	{
    		public void run()
    		{
    			try{
    				Thread.sleep(1000);
    			}
    			catch(Exception ex)
    			{
    				throw new RuntimeException(ex);
    			}
    		}
    	}
    	
    }
    Last edited by Razee Marikar; 07-22-2009 at 05:56 AM.

Similar Threads

  1. Thread Sync Problems
    By billysdomain in forum C++
    Replies: 6
    Last Post: 03-13-2009, 08:43 AM
  2. Serious problem with a website!
    By Loham in forum Java
    Replies: 1
    Last Post: 06-13-2008, 09:11 AM
  3. Problem with stored procedure
    By yip in forum Database
    Replies: 1
    Last Post: 09-24-2002, 01:31 AM
  4. Method Invoking and SyncLock?
    By Vinny in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 01:12 PM
  5. Replies: 2
    Last Post: 10-27-2000, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center