Chapter 12
Multithreading in Java
Multitasking
Performing two or more task at same time is known as multi tasking.
Two type of multi-tasking
Process based multitasking (PBM):-
Two or more program (processes) can run simultaneously is called PBM
Ex: - Running VLC, Scanning for virus, working with internet etc…..
Thread based multitasking (TBM)
Single program performing more than one task at the same time is known as thread based
multitasking.
Ex: - An MS word program can check the spelling of word while we type the document.
Advantages of TBM over PBM
❖ Threads are lightweight compared to process.
❖ TBM share the same address space and therefore share both data and code.
❖ TBM is less expensive.
❖ Cost of thread intercommunication is low.
❖ Threads allow different tasks to be performed concurrently.
Process
A process is a program executing within its own address space.
Threads
Thread is a part of a program that is running.
Thread is a single line of execution.
Threads are lightweight because they utilize minimum resources of the system. That means they
takes less memory and execution time.
Type of threads
1. Daemon threads
2. User threads
Daemon thread: usually designed to run in the background for the purpose of servicing user
thread.
Eg: garbage collector thread
User thread: -User threads are high priority threads always run in foreground. Most user threads
are created by programmer. The main thread is a user thread created by JVM. From the main
thread all other threads are created.
Type of threads
Creating threads in Java
A thread in Java is represented by an object of Thread class. The threads can be created in two
ways
By extending the java.lang.Thread class
By implementing the java.lang.Runnable interface
By extending the java.lang.Thread class
Define a class that extends thread class and over write run
class MyThread extends Thread
{
public void run ( )
{
System.out.println (“A thread is running”);
}
}
public void run(): is used to perform action for a thread. The run method is the core part of thread
in java. This methods is entry point and exit point for any thread we create. The advantage of this
approach is that we can directly make use of all the methods in Thread class
By extending the java.lang.Thread class
Define a class that extends thread class and over write run
class MyThread extends Thread
{
public void run ( )
{
System.out.println (“A thread is running”);
}
}
public void run(): is used to perform action for a thread. The run method is the core part of thread
in java. This methods is entry point and exit point for any thread we create. The advantage of this
approach is that we can directly make use of all the methods in Thread class
By implementing the java.lang.Runnable interface
public class Main implements Runnable
{
public void run()
{
System.out.println("This code is running in a thread");
}
}
We can create thread by implementing runnable interface.
It will execute the code defined in public void run().
The advantage of this approach over the other approach is that programmer can use threads as well
as extend other class.
Creating multiple threads
When we need to perform several tasks at a time, we can create multiple threads to perform
multiple tasks in a program.
Creating multiple threads in Java programming helps to perform more than one task
simultaneously.
It improves the performance of CPU because CPU is not idle if other threads are waiting to get some
resources.
Life Cycle of a thread in Java
A thread in Java at any point of time exists in any one of the following states
Life Cycle of a thread in Java
1. New
2. Runnable
3. Waiting
4. Timed Waiting
5. Terminated
Life Cycle of a thread in Java
New Thread: When a new thread is created, it is in the new state. The thread has not yet started to run when
the thread is in this state.
Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is
considered to be executing its task.
Running: When the thread gets the CPU, it moves from the runnable to the running state. Generally, the
most common change in the state of a thread is from runnable to running and again back to runnable.
Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to
perform a task. Thread transitions back to the runnable state only when another thread signals the waiting
thread to continue executing
Terminated (Dead): This is the state of a dead thread. It's in the TERMINATED state when it has either
finished execution or was terminated abnormally.
End of Chapter