Create Thread In Java, MultiThreading In Java, join( ) And isAlive( ) In Java Explain With Program

How To Create Thread In Java, MultiThreading In Java, join( ) And isAlive( ) In Java Explain With Program

In This Topic & Blog Having Any Query Then Post your Comment and Suggestion Below

Creating a Thread
In the most general sense, you create a thread by instantiating an object of type
Thread
.
Java defines two ways in which this can be accomplished:
• You can implement the
Runnable
interface.
• You can extend the
Thread
class, itself.
The following two sections look at each method, in turn.
Implementing Runnable
The easiest way to create a thread is to create a class that implements the
Runnable
interface.
Runnable
abstracts a unit of executable code. You can construct a thread on any object that
implements
Runnable
.
To implement
Runnable
, a class need only implement a single method
called
run( )
, which is declared like this:
public void run( )
Inside
run( )
, you will define the code that constitutes the new thread. It is important to
understand that
run( )
can call other methods, use other classes, and declare variables, just
like the main thread can. The only difference is that
run( )
establishes the entry point for
another, concurrent thread of execution within your program. This thread will end when
run( )
returns.


        
After you create a class that implements
Runnable
, you will instantiate an object of type
Thread
from within that class.
Thread
defines several constructors. The one that we will use
is shown here:
Thread(Runnable  threadOb , String  threadName )
In this constructor,  threadOb  is an instance of a class that implements the
Runnable
interface.
This defines where execution of the thread will begin. The name of the new thread is specified
by  threadName .
After the new thread is created, it will not start running until you call its
start( )
method,
which is declared within
Thread
.
In essence,
start( )
executes a call to
run( )
.
The
start( )
method is shown here:
void start( )
Here is an example that creates a new thread and starts it running:
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
Inside
NewThread
’s constructor, a new
Thread
object is created by the following
statement:
t = new Thread(this, "Demo Thread");
Passing
this
as the first argument indicates that you want the new thread to call the
run( )
method on
this
object. Next,
start( )
is called, which starts the thread of execution beginning
at the
run( )
method. This causes the child thread’s
for
loop to begin. After calling
start( )
,
NewThread
’s constructor returns to
main( )
. When the main thread resumes, it enters its
for
loop. Both threads continue running, sharing the CPU, until their loops finish. The output
produced by this program is as follows. (Your output may vary based on processor speed
and task load.)
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
As mentioned earlier, in a multithreaded program, often the main thread must be the
last thread to finish running. In fact, for some older JVMs, if the main thread finishes before
a child thread has completed, then the Java run-time system may “hang.” The preceding
program ensures that the main thread finishes last, because the main thread sleeps for 1,000
milliseconds between iterations, but the child thread sleeps for only 500 milliseconds. This
causes the child thread to terminate earlier than the main thread. Shortly, you will see a
better way to wait for a thread to finish.
Extending Thread
The second way to create a thread is to create a new class that extends
Thread
, and then to
create an instance of that class. The extending class must override the
run( )
method, which
is the entry point for the new thread. It must also call
start( )
to begin execution of the new
thread. Here is the preceding program rewritten to extend
Thread:

// Create a second thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
This program generates the same output as the preceding version. As you can see, the child
thread is created by instantiating an object of
NewThread
, which is derived from
Thread
.
Notice the call to
super( )
inside
NewThread
.
This invokes the following form of the
Thread
constructor:
public Thread(String  threadName )
Here,  threadName  specifies the name of the thread.


Choosing an Approach
At this point, you might be wondering why Java has two ways to create child threads, and
which approach is better. The answers to these questions turn on the same point. The
Thread
class defines several methods that can be overridden by a derived class. Of these methods,
the only one that  must  be overridden is
run( )
. This is, of course, the same method required
when you implement
Runnable
.
Many Java programmers feel that classes should be
extended only when they are being enhanced or modified in some way. So, if you will not
be overriding any of
Thread
’s other methods, it is probably best simply to implement
Runnable
. This is up to you, of course. However, throughout the rest of this chapter, we
will create threads by using classes that implement
Runnable
.
Creating Multiple Threads
So far, you have been using only two threads: the main thread and one child thread. However,
your program can spawn as many threads as it needs. For example, the following program
creates three child threads:
// Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");


        
try {
// wait for other threads to end
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
The output from this program is shown here:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Two exiting.
Three exiting.
Main thread exiting.
As you can see, once started, all three child threads share the CPU. Notice the call to
sleep(10000)
in
main( )
. This causes the main thread to sleep for ten seconds and ensures
that it will finish last.
Using isAlive( ) and join( )
As mentioned, often you will want the main thread to finish last. In the preceding examples,
this is accomplished by calling
sleep( )
within
main( )
, with a long enough delay to ensure
that all child threads terminate prior to the main thread. However, this is hardly a satisfactory
solution, and it also raises a larger question: How can one thread know when another thread
has ended? Fortunately,
Thread
provides a means by which you can answer this question.


Two ways exist to determine whether a thread has finished. First, you can call
isAlive( )
on the thread. This method is defined by
Thread
, and its general form is shown here:
final boolean isAlive( )
The
isAlive( )
method returns
true
if the thread upon which it is called is still running. It returns
false
otherwise.
While
isAlive( )
is occasionally useful, the method that you will more commonly use to
wait for a thread to finish is called
join( )
, shown here:
final void join( ) throws InterruptedException
This method waits until the thread on which it is called terminates. Its name comes from the
concept of the calling thread waiting until 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.
Here is an improved version of the preceding example that uses
join( )

 to ensure that the
main thread is the last to stop. It also demonstrates the
isAlive( )
method.

// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
Sample output from this program is shown here. (Your output may vary based on processor
speed and task load.)
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2


One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.
As you can see, after the calls to
join( )
return, the threads have stopped executing.

Labels: