Java supports multithreaded programming. Threads means part of the program which runs concurrently and which defines separate part of execution. Threads are present in many states.It can be running or it can be made ready to run as soon as it gets CPU time. A running thread can be suspended temporarily and also can be resumed. Thread can also be blocked, it can also be terminated in which execution is stopped. When thread is terminated it cannot be resumed.
As Java supports multithreading there should be synchronisation between two threads it should be ensured that do not conflict each other while sharing a complicated data structure . So Java provides object synchronized methods. Once a thread is inside a synchronized method,no other thread can call any other method on the same object.
When we divide out program in different threads it is necessary to have communication between them many other languages depend on the operating systems but in Java it allows a thread to enter a synchronized method on an object and then wait there till some other thread tells it to come out.Every thread in Java is created and controlled by the java.lang.Thread class.
While creating a Thread in Java it can be defined in two ways
1. By implementing the Runnable interface
2. One can extend the thread class itself.
Runnable interface
To create a thread one has to create a class that implements a Runnable interface. To implement a Runnable a class need only implement a single method call run(), which is declared like this:
public void run()
Inside run() one has define code for new thread. It is important to know that run() can call other methods and work with various classes and variables. Once you create a class which implements Runnable then you have to create an object of type thread of that class. Once the new thread it will start running after you call start() method within the same thread.
It can be declared in this way
void start()
Let us see an example of how to create a new thread
class NewThread implements Runnable {
Thread n;
NewThread () {
// New thread created here
n = new thread(this,”Thread1″);
System.out.println(“Second Thread: ” + n);
//start the new thread.
n.start ();
}
public void run () {
try {
for (int i = 5; i > 0; i–) {
System.out.println(“Second Thread: ” + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println(“Second Thread Interrupted”);
}
System.out.println(“Exiting Second Thread”);
}
}
class ThreadDemo {
public static void main(String args[]) {
//New thread created here
new NewThread();
try {
for (int i=5; i>0; i–) {
System.out.println(“First Thread: ” + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(“First thread Interrupted”);
}
System.out.println(“Exiting First Thread”);
}
}
Passing this as argument means that you want the new thread to call run() method and then start() method. Start() then starts the execution of the thread which causes Second Thread’s loop to begin. After start() method is called NewThread’s constructor returns to main(). When First thread resumes it enters its for loop.
The output of the above program is :-
Second Thread: Thread[Thread1,5,main]
First Thread:5
Second Thread:5
Second Thread:4
First Thread:4
Second Thread:3
Second Thread:2
First Thread:3
Second Thread:1
Exiting Second Thread
First Thread:2
First Thread:1
Exiting First Thread
If the First Thread finishes before a Second Thread has finished then Java run-time system may hang so First Thread sleeps for 1000 milliseconds and Second Thread sleeps for 500 milliseconds.This helps Second Thread to terminate earlier than the First Thread.
Extending Thread
The second way to create a thread is to create a new class that extends thread and then create an instance of that class. The extending class must override the method the run() method.We should also call start() to begin the execution of new thread.
Let us see an example of Extending Thread
//create a thread
class NewThread extends Thread {
NewThread() {
super(“thread1″);
System.out.println(“Second Thread: ” + this);
//start the thread
start();
}
public void run () {
try {
for (int i=5; i > 0; i–) {
System.out.println(“Second Thread: ” + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println(“Second Thread Interrupted”);
}
System.out.println(“Exiting Second Thread”);
}
}
class ExtendThread {
public static void main(String args[]) {
//New thread created here
new NewThread();
try {
for (int i=5; i>0; i–) {
System.out.println(“First Thread: ” + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(“First thread Interrupted”);
}
System.out.println(“Exiting First Thread”);
}
}
The output of the program is same as the earlier one . The call of super() inside NewThread will invoke the Thread constructor.
If you enjoyed this post, make sure you subscribe to my RSS feed!
