The Java Thread Model:
The java programming language allows us to create a program that contains one or more parts that can run simultaneously at the same time. This type of program is known as a multithreading program. Each part of this program is called a thread.
A multithreaded program contains many threads. Threads are Java’s way of running multiple parts of a program simultaneously. The process of executing multiple threads simultaneously is known as multithreading.
Every thread defines a separate path of execution in java. A thread is a light weight process. They are smaller unit of a running process.
A thread may also be defined as follows:
A thread is a subpart of a process that can run individually.
Life Cycle of a Thread:
In java, a thread goes through different states throughout its execution. These stages are called thread life cycle states or phases. A thread may in any of the states like new, ready or runnable, running, blocked or wait, and dead or terminated state. The life cycle of a thread in java is shown in the following figure:
Newborn State:
When a thread object is created using new, then the thread is said to be in the New state. This state is also known as Born state.
Example:
Thread t1 = new Thread();
Runnable / Ready State:
When a thread calls start( ) method, then the thread is said to be in the Runnable state. This state is also known as a Ready state.
Example:
t1.start( );
Running State:
When a thread calls run( ) method, then the thread is said to be Running. The run( ) method of a thread called automatically by the start( ) method.
Blocked / Waiting State:
A thread in the Running state may move into the blocked state due to various reasons like sleep( ) method called, wait( ) method called, suspend( ) method called, and join( ) method called, etc.
When a thread is in the blocked or waiting state, it may move to Runnable state due to reasons like sleep time completed, waiting time completed, notify( ) or notifyAll( ) method called, resume( ) method called, etc.
Example:
Thread.sleep(1000);
wait(1000);
wait();
suspened();
notify();
notifyAll();
resume();
Dead / Terminated State
A thread in the Running state may move into the dead state due to either its execution completed or the stop( ) method called. The dead state is also known as the terminated state.
Understanding Threads:
Threads are Java’s way of running multiple parts of a program simultaneously. Java provides built-in support for multithreaded programming. A multithreaded program contains many threads. A thread is like a program executing a set of instructions in sequence from within a program. However, a thread cannot run on its own. A Java program during its execution can execute many threads, which can share data and run asynchronously. This way many things can be done at the same time.
Creating a Thread:
Creating thread in Java is simple. A thread can be created in two ways:
· By extending the Thread class
· By implementing Runnable interface.
By extending the Thread class:
class Multithread extends Thread
{
public void run()
{
System.out.println(“My thread is running”);
}
public static void main(“String args[])
{
Multithread obj=new Multithread();
obj.start();
}
}
Output:
My thread is running
By implementing Runnable interface:
class Multithread implements Runnable
{
public void run()
{
System.out.println(“My thread is running”);
}
public static void main(“String args[])
{
Multithread obj=new Multithread();
Thread tobj=new Thread(obj);
obj.start();
}
}
Output:
My thread is running
Creating Multiple Threads:
The java program may create any number of threads. Normally, multiple threads are used for animation. Multiple threads can access the same object or method. There is no guarantee which thread will access an object at a given time, which can lead to unpredictable results. In the case of two or three threads sharing the same object, then only one thread must access the object at a time. In order to do this, java synchronization logic can be applied which is make use of the keyword, synchronized, with which an object can be locked to execute a block of code preventing other threads from accessing that particular object. That is, the method takes control of the “monitor” of the object.
Thread Priorities:
Java assigns a priority to each thread that determines how that thread should be treated with respect to others. A higher priority thread does not run faster than a lower priority thread. Instead, a thread’s priority is used to decide when to switch from one running thread to the next.
Thread priorities can be changed according to the importance of the task that the thread is executing.
In Java, each thread is assigned a different priority that will decide the order (preference) in which it is scheduled for running.
Thread priorities are represented by a number from 1 to 10 that specify the relative priority of one thread to another. The thread with the highest priority is selected by the scheduler to be executed first.
The default priority of a thread is 5. Thread class in Java also provides several priority constants to define the priority of a thread. These are:
1. MIN_PRIORITY = 1
2. NORM_PRIORITY = 5
3. MAX_PRIORTY = 10
When multiple threads are ready for execution, the highest priority thread is selected and executed by JVM. In case when a high priority thread stops, yields, or enters into the blocked state, a low priority thread starts executing.
If any high priority thread enters into the runnable state, it will preempt the currently running thread forcing it to move to the runnable state. The highest priority thread always preempts any lower priority thread.
Priority is set using the method, setPriority(n) where n is an integer ranging from 1 to 10. The current priority of a thread can be got by using the getPriority()method.