Professional Documents
Culture Documents
PROGRAMMING
MultiThreading
Learning Outcomes
1. Define thread and its uses in Java program.
2. Explain the different types of thread:
a. Single thread
b. Multiple thread
3. Differentiate between multitasking and
multithreading.
4. List the methods involved in the life cycle of a
thread.
5. Write multithreaded application.
6. Apply the concept of Runnable Interface.
7. Integrate the concept of threading with GUI.
Thread
• Thread can be defined as single, sequential flow of control
within a program.
• A thread is a lightweight sub process, a smallest unit of
processing. It is a separate path of execution
• Threads are independent, if there occurs exception in one
thread, it doesn't affect other threads. It shares a common
memory area.
• Two types:
1. Single thread
2. Multiple threads
Multithreading
• Multithreading in java is a process of
executing multiple threads simultaneously.
• Multiprocessing and multithreading, both are
used to achieve multitasking.
• But we use multithreading than
multiprocessing because threads share a
common memory area.
• Java Multithreading is mostly used in games,
animation etc.
12/1/2021
Advantages of Multithreading
• It doesn't block the user because threads are
independent and you can perform multiple
operations at same time.
• You can perform many operations together
so it saves time.
• Threads are independent so it doesn't affect
other threads if exception occur in a single
thread.
12/1/2021
Single threaded Application
• A task or process that is made up of only
one thread is single-thread.
• A program that supports single thread is
called single threaded application.
Multi-threaded Application
• A task or process that is made of more
than one thread is multiple threading.
• A program that supports more than one
thread is multi-threaded application
Multi-tasking
• Is the ability of an operating system to
execute more than one program
simultaneously.
• Multitasking can be achieved by two ways:
– Process-based Multitasking(Multiprocessing)
– Thread-based Multitasking(Multithreading)
Difference Between Multiprocessing and Multithreading
No Multiprocessing Multithreading
………………..
………………..
………………..
Main Thread
Thread 1 Thread 2
Declaring a Thread class – General Syntax
class <new thread> extends Thread
{
……………..
……………..
……………..
……………..
……………..
}
Declaring a Thread Class - Example
Syntax
Threadname objectname = new Threadname;
objectname.start();
Implementing the run() method
public void run()
{
……………
……………
……………
……………
……………
}
Stopping and Blocking Threads
A thread during the running state can be moved
to the not runnable state. This can be achieved
by:
• Stopping a thread
• Blocking a thread
Stopping a Thread - Syntax
objectname.stop();
obj.stop();
Blocking a Thread
A thread can be temporarily suspended or blocked
from running. The thread is blocked from entering
the runnable or running state. A thread can be
blocked using any of the three methods.
sleep() - The thread is blocked for a specified time.
The thread returns to the runnable state when
the specified time is elapsed.
suspend() - The thread is suspended until further
ordered. The resume() method is called.
wait() - The thread is blocked until a condition
occurs. The notify() method is called.
Methods in Thread Class
Methods Description
12/1/2021
Java Thread Example by Implementing
Runnable Interface
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}