Multithreading in
Java
Lab outcome: Implement multithreading in
Java by creating and executing threads
using the Thread class and Runnable
interface.
Company Name / Year / Q1 - Month
Interface
MULTI-THREADING
Multithreading in Java is a feature that allows multiple
tasks to run concurrently within the same program. Instead
of executing one task at a time, Java enables parallel
execution using lightweight threads. This makes
applications more efficient, faster and responsive in
real-world scenarios like servers, games and chat systems.
[Link]
Features
● A thread is the smallest unit of execution in Java.
● Threads share the same memory space but run independently.
● Java provides the Thread class and Runnable interface to create
threads.
● Multithreading ensures better CPU utilization by executing tasks
simultaneously.
● Synchronization is needed to prevent data inconsistency when
threads share resources.
Process Thread
A process is an independent program in execution. A thread is the smallest unit of execution within
a process.
Has its own separate memory space. Shares the memory of its parent process.
Creation is slower and requires more system Creation is faster and requires fewer resources.
resources.
Communication between processes is slower Communication between threads is faster
(Inter-Process Communication). because they share memory.
Failure of one process usually does not affect other Failure of one thread can affect the entire
processes. process.
Process switching is slower (context switching is Thread switching is faster (lighter context
expensive). switching).
A process may contain one or more threads. A thread cannot exist without a process.
Example: Google Chrome, MS Word, VLC Media Example: Spell Checker, Auto Save, File
Player. Download within MS Word.
Normal (User) Thread Daemon Thread
Performs the main task of the application. Performs background or support tasks.
Created by default when a thread is created. Must be explicitly marked using setDaemon(true).
JVM waits for all user threads to finish before JVM does not wait for daemon threads; they terminate
automatically when all user threads finish.
terminating.
Keeps the application running. Cannot keep the application running.
Used for important tasks such as calculations, Used for background services like garbage
user requests, or file processing. collection, monitoring, and logging.
Default status is non-daemon (false). Status is daemon (true).
Continues execution until its task is May stop abruptly if all user threads have
completed. finished.
Examples: Main thread, file download, Examples: Garbage Collector, auto-save
banking transaction. service, cache cleaner.
Java Threads
Java thread is the smallest unit of execution within a program. It is a lightweight subprocess that runs
independently but shares the same memory space as the process, allowing multiple tasks to execute
concurrently.
● Extending Thread Class
● Implementing a Runnable interface
Company Name / Year / Q1 - Month
Java Threads
1. By Extending Thread Class
Create a class that extends Thread. Override the run() method, this is where you put
the code that the thread should execute. Then create an object of your class and call
the start() method. This will internally call run() in a new thread.
Company Name / Year / Q1 - Month
Java Threads
2. Using Runnable Interface
Create a class that implements Runnable. Override the run() method, this contains
the code for the thread. Then create a Thread object, pass your Runnable object to it
and call start().
Company Name / Year / Q1 - Month
Lifecycle
During its thread life cycle, a Java thread transitions through several states from
creation to termination.
New State
Runnable State
Blocked State
Waiting State
Timed Waiting State
Terminated State
Company Name / Year / Q1 - Month
Running Threads in Java
There are two methods used for running Threads in Java:
run() Method: Contains the code for the thread. Calling it directly behaves like
a normal method call.
start() Method: Launches a new thread and internally calls run() concurrently.
Company Name / Year / Q1 - Month
Program 1: Create a thread using Thread Class
Program 2: Create a thread using Runnable
Interface
Program 3: Create a thread using Thread Class
and Runnable Interface
Company Name / Year / Q1 - Month
Running Threads in Java
[Link]
Company Name / Year / Q1 - Month
Program 4:Create a multithreading program
using the Thread class to display numbers
and alphabets simultaneously with proper
thread synchronization.
Company Name / Year / Q1 - Month
Program 5: Develop a Java program using
the Runnable interface to simulate a railway
reservation system demonstrating thread
synchronization