Results 1 to 5 of 5
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default wait() vs join()

    Hi friends,

    i am confused with the difference between wait() and join() methods.
    i know join is from Thread class and wait from Object class. but where to use one than another?
    Java Code:
    class Thread1 implements Runnable
    {
    public void run()
    	{
    	System.out.println("Hai from Thread1");
    	}
    }
    
    class Wait_Notify
    {
    public static void main(String ... args)
    	{
    	System.out.println("main Begins");
    	Thread t = new Thread(new Thread1());
    	t.start();
    	try
    	{
    	synchronized(t){
    		t.wait();----------------------------------#1
    		}
    	}
    	catch(InterruptedException e)
    	{
    	}
    	System.out.println("Main ends");
    	}
    }
    if i change this wait() to join() no change in output.
    Last edited by jomypgeorge; 01-20-2011 at 10:02 AM. Reason: wrong spelling

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

    Default

    If a Thread waits on a certain object X it can be kicked out of its idle state by another Thread that does a notify(All) on that object X and both Threads can run happily ever after. When a Thread joins another Thread it is only woken up when that other Thread dies. In your example Thread1 dies after printing to the console so any other Thread wakes up afterwards. My guess is that if a Thread waits on Thread1 it will be 'spuriously' woken up (or interrupted).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks josAH..
    so we need to use wait() and notify() only if the invoked thread need to communicate with invoking thread.
    also waiting on a thread which never notify() is foolish, and we can use join() for that.
    Last edited by jomypgeorge; 01-20-2011 at 01:24 PM.

  4. #4
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    am i correct josAH

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

    Default

    Quote Originally Posted by jomypgeorge View Post
    am i correct josAH
    Yes, you are correct; also read the API documentation for the classes and interfaces in the java.util.concurrent package. They implement quite a lot of functionality used in multi threaded program and take away a lot of the nitty gritty details from you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java - hibernate query with join
    By dergotic in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2010, 09:02 PM
  2. Join two images
    By forumgopi in forum Java 2D
    Replies: 0
    Last Post: 05-14-2010, 05:40 AM
  3. Regarding Join
    By adeeb in forum JDBC
    Replies: 1
    Last Post: 06-24-2008, 07:57 PM
  4. How to use join() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:33 PM
  5. i'm happy to join this forum
    By sivasayanth in forum Introductions
    Replies: 1
    Last Post: 12-25-2007, 08:56 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
  •