By: Pankaj Yadav
Multi threading Programs in Java
Multithreading is a conceptual programming paradigm where a program/process is divided into two or more subprograms/processes, which can be implemented at the same time or in parallel.
A thread is similar to a program which has its beginning, body and end and executes command statements sequentially.
1. Creating threads
The main method on which the thread depends is the run method in which we can implement the main work of thread. But the thread can only be initialized with the help of the method start().
A thread can be created in the two ways
- i. By creating thread class: in this we have to define a class that extends the Thread class and have to override the run() method
- ii. By converting the class to thread: Defining a class which implements the run able interface.
Life Cycle of thread
There are many states in the life cycle of threads. The states are:
1. New born State: It is the state when the thread is not yet scheduled to running. But, at this state the instance of the thread got initialized in the memory. We can perform following things at this state:
- a. We can schedule running of thread in this state with the help of start() method.
- b. We can end the thread with the help of kill() method.
2. Runnable state: It is the state when the instance is get into the queue of process and waits for execution. If we want a thread relinquish control to other thread to another thread before its turn, it can be done with the help of yield() method.
3. Running state: It is the state when the thread is running. And processor gave time to the thread for its execution. A running thread may relinquish its control in the following conditions.
- a. Suspend(): This is useful if we want to suspend a thread for some time .And we can resume thread with the help of resume() method.
- b. Sleep(): We can put thread on sleep for some milliseconds. It invokes as soon as the time elapsed.
- c. wait(): We can put thread on wait until any occurs this can be done by wait() method. And we can reschedule running using the notify() method
4. Blocked state: It is the state where the thread is being stopped form being running.
5. Dead state: It is the state when the execution of run method being completed.
Thread priority
Each thread is assigned with a priority, which affects the order in which schedule is running. Java permits to set priority using the setPriority() method. The thread priority is set using integer value.
The thread class defines following constant priorities:
MIN_PRIORITY =1
NOR_PRIORITY =5
MAX_PRIORITY =10
Multi Thread Example
class TestA extends Thread {
public void run() {
System.out.println("TestA Thread started");
for (int i = 1; i <= 4; i++) {
System.out.println("\t From thread TestA :i=" + i);
}
System.out.println("exit from TestA");
}
}
class TestB extends Thread {
public void run() {
System.out.println("TestB Thread started");
for (int j = 1; j <= 4; j++) {
System.out.println("\t From thread TestB :j=" + j);
}
System.out.println("exit from TestB");
}
}
class TestC extends Thread {
public void run() {
System.out.println("testC Thread started");
for (int k = 1; k <= 4; k++) {
System.out.println("\t From thread TestC :K=" + k);
}
System.out.println("exit from TestC");
}
}
public class TestThread {
public static void main(String args[]) {
TestA A = new TestA();
TestB B = new TestB();
TestC C = new TestC();
C.setPriority(Thread.MAX_PRIORITY);
B.setPriority(Thread.NORM_PRIORITY+1);
A.setPriority(Thread.MIN_PRIORITY);
System.out.println(" Begin thread A");
A.start();
System.out.println(" Begin thread B");
B.start();
System.out.println(" Begin thread C");
C.start();
System.out.println(" End of main thread");
}
}
Output
Begin thread A
Begin thread B
TestA Thread started
Begin thread C
From thread TestA :i=1
testC Thread started
From thread TestC :K=1
End of main thread
From thread TestC :K=2
From thread TestC :K=3
From thread TestC :K=4
exit from TestC
From thread TestA :i=2
From thread TestA :i=3
TestB Thread started
From thread TestB :j=1
From thread TestB :j=2
From thread TestB :j=3
From thread TestB :j=4
exit from TestB
From thread TestA :i=4
exit from TestA



Link to Us