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.
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.
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.
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");
}
}
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.
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.");
}
}