Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



join() and isAlive() methods of java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-01-2010
Member
 
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.

Reply With Quote
  #2  
Old 21-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,947
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.
Reply With Quote
  #3  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,007
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.
Reply With Quote
  #4  
Old 21-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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");

    }

}
Reply With Quote
  #5  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,290
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.
Reply With Quote
  #6  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,382
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.");
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "join() and isAlive() methods of java"
Thread Thread Starter Forum Replies Last Post
Identify active methods in java Maya Angelou Software Development 4 06-04-2010 01:53 PM
What is Export Methods in Java? NIcaBoy Software Development 4 13-02-2010 05:29 AM
Explain the difference between == & .equals methods in Java? Harpreet Gaur Software Development 5 30-01-2010 10:36 AM
Native methods of java Truster Software Development 5 22-01-2010 09:52 AM
Methods in JAVA Caiden Software Development 3 18-08-2009 11:26 PM


All times are GMT +5.5. The time now is 12:33 PM.