Results 1 to 6 of 6

Thread: join() and isAlive() methods of java

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    join() and isAlive() methods of java

    Hi Friends,

    I am beginner in the java programming field. I have sound knowledge about C and C++. But I am getting difficulties while understanding the join() and isAlive() methods of java. I don't have any idea about the exact use of these join() and isAlive() methods of java. If anybody has the knowledge about these java methods then please let me know that. I wonder if you are able to explain with suitable eaxmple.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Re: join() and isAlive() methods of java

    join( ) method of java waits till the thread on which it is called terminates. The isAlive( ) method of java returns true only if the thread is still running. The name of this java method comes from the concept of the calling the java thread waiting till the specified thread joins it. Additional forms of join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate.

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    Re: join() and isAlive() methods of java

    Hi friend,

    The java 'isAlive' method tests if this thread is alive. A thread is alive if it has been started and has not yet died. This java method returns true only if this thread is alive; otherwise it returns false.The java 'join' method Waits at most mills milliseconds for this thread to die. A timeout of 0 means to wait forever. This method throws 'InterruptedException' only if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    Re: join() and isAlive() methods of java

    Hi,

    Refer the following java example to understand the isAlive() method of java:
    Code:
    public class ThreadIsAlive1 implements Runnable {
    
        private int countDown1 = 5;
    
      
        public ThreadIsAlive1() {
            System.out.println("Thread constructor...\n");
        }
    
       
        public void run1() {
    
            while(true) {
    
                System.out.println("  - Thread ( Current Countdown = " + countDown + " )");
    
                for (int j = 0; j < 300000000; j++) {
                   
                }
               
                if (--countDow1n == 0) {
                    System.out.println("\nEnding thread...\n");
                    return;
                }
            }
        }
    
    
       
        private static void doThreadTest1()
            throws java.lang.InterruptedException {
    
            int checkCount1 = 0;
    
            Thread th1 = new Thread(new ThreadIsAlive1());
            th1.start();
    
            while (th1.isAlive()) {
                Thread.sleep(500);
                System.out.println("Thread is still alive. Count = " + ++checkCount);
            }
    
            System.out.println("\n Finished running and checking thread!!! >>\n");
        }
    
    
        public static void main(String[] args)
            throws java.lang.InterruptedException {
    
            System.out.println("\n MAIN METHOD (Begin)>");
           
            doThreadTest1();
           
            System.out.println(" MAIN METHOD (End) \n");
    
        }
    
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    Re: join() and isAlive() methods of java

    The java programmers use two methods for ensuring the main thread of program is the last thread to terminate. These involve to call join() and isAlive() method. The isAlive() method returns a Boolean true value. Both join and isAlive methods are defined within the 'Thread class'. You can use the isAlive() method to examine whether a child thread continues to run. The join() method works differently than the isAlive() method. The isAlive() method determines whether a thread is still running.

  6. #6
    Join Date
    May 2008
    Posts
    2,393

    Re: join() and isAlive() methods of java

    Hello friend,

    I have one java program which will depicts you the declaration of the join() method and isAlive() method of java. These two java methods are basically related to the threads. See below:
    Code:
    class DemoJoin2 {
    public static void main(String args[]) {
    NewThread ob21 = new NewThread("One");
    NewThread ob22 = new NewThread("Two");
    NewThread ob23 = new NewThread("Three");
    System.out.println("Thread One is alive: "+ ob21.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob22.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
    
    try {
    System.out.println("Waiting for threads to finish.");
    ob21.t.join();
    ob22.t.join();
    ob23.t.join();
    } catch (InterruptedException e) {
    System.out.println("Main thread Interrupted");
    }
    System.out.println("Thread One is alive: "+ ob21.t.isAlive());
    System.out.println("Thread Two is alive: "+ ob22.t.isAlive());
    System.out.println("Thread Three is alive: "+ ob23.t.isAlive());
    System.out.println("Main thread exiting.");
    }
    }

Similar Threads

  1. Identify active methods in java
    By Maya Angelou in forum Software Development
    Replies: 4
    Last Post: 06-04-2010, 01:53 PM
  2. What is Export Methods in Java?
    By NIcaBoy in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 05:29 AM
  3. Explain the difference between == & .equals methods in Java?
    By Harpreet Gaur in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 10:36 AM
  4. Native methods of java
    By Truster in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:52 AM
  5. Methods in JAVA
    By Caiden in forum Software Development
    Replies: 3
    Last Post: 18-08-2009, 11:26 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
  •