001 Multithreading
001 Multithreading
Introduction
However, we use multithreading than multiprocessing because threads use a shared memory
area. They don't allocate separate memory area so saves memory, and context-switching
between the threads takes less time than process.
Page 1
Advantage of Java Multithreading
1) It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together, so it saves time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs
in a single thread.
Thread States
In Java, a thread always exists in any one of the following states. These states are:
1. New
2. Active/Runnable
3. Blocked/waiting
4. Timed Waiting
5. Terminated
Page 2
1. New Thread: When a new thread is created, it is in the new state. The thread has
not yet started to run when thread is in this state. When a thread lies in the new state,
its code is yet to be run and hasn’t started to execute.
2. Runnable State: A thread that is ready to run is moved to runnable state. In this
state, a thread might actually be running or it might be ready run at any instant of time.It
is the responsibility of the thread scheduler to give the thread, time to run.
A multi-threaded program allocates a fixed amount of time to each individual thread.
Each and every thread runs for a short while and then pauses and relinquishes the CPU
to another thread, so that other threads can get a chance to run. When this happens, all
such threads that are ready to run, waiting for the CPU and the currently running thread
lies in runnable state.
3. Blocked/Waiting state: When a thread is temporarily inactive, then it’s in one ofthe
following states: 1. Blocked
2. Waiting
For example, when a thread is waiting for I/O to complete, it lies in the blocked state. It’s
the responsibility of the thread scheduler to reactivate and schedule a blocked/waiting thread.
A thread in this state cannot continue its execution any further until it is moved to Runnable
state. Any thread in these states does not consume any CPU cycle.
A thread is in the blocked state when it tries to access a protected section of code that is
currently locked by some other thread. When the protected section is unlocked, the schedule
picks one of the threads which is blocked for that section and moves it to the runnable state.
Whereas, a thread is in the waiting state when it waits for another thread on a condition.
When this condition is fulfilled, the scheduler is notified and the waitingthread is moved to
runnable state.
4. Timed Waiting: A thread lies in timed waiting state when it calls a method with a
time out parameter. A thread lies in this state until the timeout is completed or untila
notification is received. For example, when a thread calls sleep or a conditionalwait,
it is moved to a timed waiting state.
Page 3
5. Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
Threads with higher priority are more important to a program and should be allocated
processor time before lower-priority threads. However, thread priorities cannot guarantee
the order in which threads execute and are very much platform dependent.
Write a class that extends Thread class or implements Runnable interface this is
available in lang package.
Write public void run () method in that class. This is the method by default executed
by any thread.
Create an object to that class.
Create a thread and attach it to the object.
Start running the threads.
Page 4
Output:
Page 5
Example 2: write program to create more than one Thread.
Output:
Page 6