Multithreading in Java
Multithreading in Java
Introduction
Process-based:
o Execution of more than one program concurrently.
o Processes are heavyweight.
o Each process has separate memory area.
o Switching of CPU from one process to another requires more
overhead as each process has separate memory area.
Thread-based:
o Executing a single program having more than one thread,
performing different tasks simultaneously.
o Threads are lightweight.
o Threads share the same address space.
o Switching of CPU occurs within the program within same address
space; require less overhead.
Every Java program has at least one thread, i.e., the main thread.
Whenever a program starts executing, the JVM is responsible for
creating the main thread and calling the main() method, from within that
thread.
A thread can either die naturally or be forced to die.
o A thread dies naturally when it exits the run() method normally.
o A thread can be killed by calling interrupt() method.
java.lang.Thread
structure of run():
Main Thread
name of the main thread can be changed from main to any new name.
Example:
package Thread_Package;
public class Demo_1 {
public static void main (String args[] ){
threadObj.setName("New Thread");
System.out.println("Renamed Thread: "
+threadObj);
}}
2. Overriding the run() Method: The run() method has to be overridden. The
thread behaves as per this code.
public void run( )
{
//code segment providing the functionality of thread
}
3. Starting New Thread: The start() method of the Thread class is used to
initiate the execution of a thread.
newThread thread1 = new newThread();
thread1.start();
2. Overriding the run() Method: The run() method has to be overridden. This
method defines what thread will execute.
4. Start the Thread Using the start() Method: Call the start() method on the
Thread object to initiate the execution of a thread.
thread.start();
Thread.State IN JAVA
New:
A thread is in the NEW state when it is created but not yet started.
Thread threadObj = new ThreadDemo();
Runnable:
Not Runnable:
From runnable state, a thread might move to the not runnable state, as
shown in the Figure.
A thread which is in any of these three states can be assumed to be in
‘not runnable’ state. These three states -WAITING,
TIMED_WAITING,BLOCKED.
Waiting:
The thread will be in waiting state when it calls wait() method or join()
method.
A thread is in the WAITING state when it is waiting indefinitely for
another thread to perform a specific action (e.g., calling notify() or
notifyAll()).
The thread remains in this state until explicitly notified.
Timed_Waiting:
Blocked:
The thread will be in blocked state when it is trying to acquire a lock on
the resource but currently the lock is acquired by the other thread.
The thread will move from the blocked state to runnable state when it
acquires the lock.
Terminated:
A Thread is in the terminated state when it has finished its execution.
In this state, the thread is dead. The death of a thread can either be
natural or forceful.
o A thread dies naturally when it exits run() normally. The normal
exit from run() means the instructions of the run() has been
processed completely.
o A thread can be forcefully killed by using interrupt().