The Java Thread Model
The Java Thread Model
JAVA CH-4
DEPARTMENT OF MCA
The Java run-time system depends on threads for many things, and all the class libraries are
designed with multithreading in mind.
A thread can be running. It can be ready to run as soon as it gets CPU time.
To create a new thread, your program will either extend Thread or implement the Runnable
interface.
The Thread class defines several methods that help manage threads few among them are:
Method
getName
getPriority
isAlive
Join
Run
Sleep
Start
Meaning
Obtain a thread's name.
Obtain a thread's priority.
Determine if a thread is still running.
Wait for a thread to terminate.
Entry point for the thread.
Suspend a thread for a period of time.
Start a thread by calling its run method.
When a Java program starts up, one thread begins running immediately that is main.
E-Mail: khutub27@gmail.com
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
Note:
Although the main thread is created automatically when your program is started, it can be
controlled through a Thread object.
To do so, you must obtain a reference to it by calling the method currentThread( ), which is a
public static member of Thread.
Its general form is shown here:
static Thread currentThread( )
This method returns a reference to the thread in which it is called. Once you have a reference
to the main thread, you can control it just like any other thread.
Example program of Main Thread:
class CurrentThreadDemo
{
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
Output of Program:
Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1
Notice the output produced when t is used as
an argument to println( ).
This displays, in order: the name of the
thread, its priority, and the name of its group.
By default, the name of the main thread is
main.
Its priority is 5, which is the default value,
and main is also the name of the group of
threads to which this thread belongs.
The sleep( ) method causes the thread from which it is called to suspend execution for the
specified period of milliseconds. Its general form is shown here:
static void sleep(long milliseconds) throws InterruptedException
The number of milliseconds to suspend is specified in milliseconds. This method may throw
an InterruptedException.
E-Mail: khutub27@gmail.com
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
The sleep( ) method has a second form, shown next, which allows you to specify the period in
terms of milliseconds and nanoseconds:
static void sleep(long milliseconds, int nanoseconds) throws InterruptedException
This second form is useful only in environments that allow timing periods as short as
nanoseconds.
In the most general sense, you create a thread by instantiating an object of type Thread.
The easiest way to create a thread is to create a class that implements the Runnable interface.
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.
After you create a class that implements Runnable, you will instantiate an object of type
Thread from within that class.
E-Mail: khutub27@gmail.com
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
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.");
}
}
Output:
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.
E-Mail: khutub27@gmail.com
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
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.");
}
}
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.
catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}} // end of NewThread class
class MultiThreadDemo {
public static void main(String args[]) {
new NewThread("One");// start threads
new NewThread("Two");
new NewThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}}
E-Mail: khutub27@gmail.com
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
OUTPUT:
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.
E-Mail: khutub27@gmail.com