Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Thread and Multi Threading in Java

KARTHIKA RN M.TECH (PhD )


ASSISTANT PROFESSOR
DEPARTMENT OF INFORMATION TECHNOLOGY
Single Thread

 A single thread is basically a lightweight and the smallest unit of processing. Java uses threads by using a "Thread Class".

daemon threads
Thread
are used when we
want to clean the
application and
are used in the
User Thread Daemon Thread background
Thread

Single Thread
User Daemon
Thread Thread

 When an application first begins, user thread is created. Post that, we can create many user threads and daemon
threads.

 Advantages of single thread:


• Reduces overhead in the application as single thread execute in the system
• Also, it reduces the maintenance cost of the application.
Example of Singl
e Thread
Multi Thread in Java
 MULTITHREADING in Java is a process of executing two or more threads simultaneously to
maximum utilization of CPU.
 Multithreaded applications execute two or more threads run concurrently. Hence, it is also
known as Concurrency in Java.
 Each thread runs parallel to each other.
 Multiple threads don't allocate separate memory area, hence they save memory. Also, context Example of
switching between threads takes less time. Mutli Thread
 Advantages of multithread:
• The users are not blocked because threads are independent, and we can perform multiple
operations at times
• As such the threads are independent, the other threads won't get affected if one thread meets
an exception.
Life cycle of a thread

1.New: In this phase, the thread is created using class "Thread class". It remains
in this state till the program starts the thread. It is also known as born thread.
2.Runnable: In this page, the instance of the thread is invoked with a start
method. The thread control is given to scheduler to finish the execution. It
depends on the scheduler, whether to run the thread.
3.Running: When the thread starts executing, then the state is changed to
"running" state. The scheduler selects one thread from the thread pool, and it
starts executing in the application.
4.Waiting: This is the state when a thread has to wait. As there multiple threads
are running in the application, there is a need for synchronization between
threads. Hence, one thread has to wait, till the other thread gets executed.
Therefore, this state is referred as waiting state.
5.Dead: This is the state when the thread is terminated. The thread is in running
state and as soon as it completed processing it is in "dead state".
Commonly used Thread
Method Description

start() This method starts the execution of the thread and JVM calls
the run() method on the thread.

Sleep(int This method makes the thread sleep hence the thread's
milliseconds) execution will pause for milliseconds provided and after that, Example of Commonly used T
again the thread starts executing. This help in synchronization hread
of the threads.

getName() It returns the name of the thread.

setPriority(int It changes the priority of the thread.


newpriority)

yield () It causes current thread on halt and other threads to execute.


Java Thread Synchronization
1.Synchronized(object)
2.{
3.//Block of statements to be synchronized
4.}
 In multithreading, there is the asynchronous behavior of the programs. If one thread is writing some data
and another thread which is reading data at the same time, might create inconsistency in the application.
 When there is a need to access the shared resources by two or more threads, then synchronization
approach is utilized.
 Java has provided synchronized methods to implement synchronized behavior.
 In this approach, once the thread reaches inside the synchronized block, then no other thread can call that
method on the same object. All threads have to wait till that thread finishes the synchronized block and
comes out of that.
 In this way, the synchronization helps in a multithreaded application. One thread has to wait till other
thread finishes its execution only then the other threads are allowed for execution.
Java Thread Synchronization

 Overriding methods run() and start() method of a runnable interface and create
two threads of that class and run them.
 Two classes,
• One which will implement the runnable interface and
• Another one which will have the main method and execute accordingly.

Example of Multi-Threading and Synchronization

You might also like