Module 5 Topic Wize Oops With Java 24-25 (2)
Module 5 Topic Wize Oops With Java 24-25 (2)
PREPARED BY
ARPITHA K M
Multithreading in Java
• Multithreading in Java is a powerful programming feature that allows
the execution of multiple threads simultaneously.
• A thread is the smallest unit of a process, and multithreading enables
the concurrent execution of two or more parts of a program to
maximize CPU utilization.
Key Concepts in Multithreading
• Thread: A thread is a lightweight subprocess, a smallest unit of
processing. Each thread in Java runs within a process.
• Main Thread: Every Java program has at least one thread, known as the
main thread. It is created automatically when the program starts.
• Concurrency: It allows multiple threads to run in overlapping time
periods, improving program efficiency.
• Parallelism: True parallel execution of threads on multi-core processors.
The Life Cycle/Model of a Thread in Java
The Life Cycle of a Thread in Java refers to the state
transformations of a thread that begins with its birth and
ends with its death.
When a thread instance is generated and executed by calling
the start() method of the Thread class, the thread enters the
runnable state.
When the sleep() or wait() methods of the Thread class are
called, the thread en
ters a non-runnable mode.
Thread returns from non-runnable state to runnable state and
starts statement execution.
The thread dies when it exits the run() process.
There are basically 4 stages in the lifecycle of a thread, as given below:
New
Runnable
Running
Blocked (Non-runnable state)
Dead
1) New State
• As we use the Thread class to construct a thread entity, the thread
is born and is defined as being in the New state.
• That is, when a thread is created, it enters a new state, but the
start() method on the instance has not yet been invoked.
Ex: Thread t = new Thread();
2) Runnable State
• A thread in the runnable state is prepared to execute the code.
When a new thread's start() function is called, it enters a runnable
state.
• In the runnable environment, the thread is ready for execution and
is awaiting the processor's availability (CPU time).
• That is, the thread has entered the queue (line) of threads waiting
for execution.
EX: t.start();
3) Running State
Running implies that the processor (CPU) has assigned a time slot to the thread for execution.
When a thread from the runnable state is chosen for execution by the thread scheduler, it
joins the running state.
In the running state, the processor allots time to the thread for execution and runs its run
procedure.
This is the state in which the thread directly executes its operations. Only from the runnable
state will a thread enter the running state.
The thread is executing its run() method.
4) Blocked State
When the thread is alive, i.e., the thread class object persists, but it cannot be selected for
execution by the scheduler. It is now inactive.
Ex: Thread.sleep();
5)Dead State
When a thread's run() function ends the execution of sentences, it automatically dies or
enters the dead state.
That is, when a thread exits the run() process, it is terminated or killed. When the stop()
function is invoked, a thread will also go dead.
Ex: t.join();
1. Extending the Thread Class
When you extend the Thread class, you create a subclass of Thread and override its run() method.
The run() method contains the code that will execute when the thread starts.
class MyThread extends Thread {
public void run() {
// Code executed by the thread
for (int i = 1; i <= 5; i++) {
System.out.println("Thread running: " + i);
try {
Thread.sleep(500); // Pausing for 500 milliseconds
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread(); // Create an instance of the thread
thread.start(); // Start the thread
}
}
2. Implementing the Runnable Interface
• When you implement the Runnable interface, you create a class that implements Runnable and provide the
thread logic inside the run() method.
• Then, you pass an instance of this class to a Thread object.
Locks at the class level, ensuring no thread can execute any static
synchronized method of the class until the lock is released.
class Counter {
private static int count = 0;
OUTPUT:Producing...
Consuming...
SUSPENDING,RESUMING,AND STOPPING
THREADS
1.Java Thread suspend() method and resume() method
The suspend() method of thread class puts the thread from
running to waiting state.
This method is used if you want to stop the thread execution and
start it again when a certain event occurs.
method allows a thread to temporarily cease execution. The
suspended thread can be resumed using the resume() method.
Use a shared flag (volatile boolean) and synchronization to
achieve thread suspension and resumption safely.
Syntax
public final void suspend()
class SuspendResumeExample implements Runnable {
private volatile boolean suspended = false;
public synchronized void suspendThread() {
suspended = true;
}
public synchronized void resumeThread() {
suspended = false;
notify(); // Notify the thread to resume
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
while (suspended) {
try {
wait(); // Wait until notified to resume
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread interrupted.");
}
}
}
System.out.println("Running: " + i);
try {
Thread.sleep(500); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Thread interrupted during sleep.");
}
}
}
}
public class ThreadControlDemo {
public static void main(String[] args) throws InterruptedException {
SuspendResumeExample example = new SuspendResumeExample();
Thread thread = new Thread(example);
thread.start();
Thread.sleep(2000); // Let the thread run for a while
System.out.println("Suspending thread...");
example.suspendThread();
Thread.sleep(2000); // Wait while the thread is suspended
System.out.println("Resuming thread...");
example.resumeThread();
}
}
OUTPUT
Running: 1
Running: 2
Suspending thread...
Resuming thread...
Running: 3
Running: 4
...
2. Stopping a Thread
Stopping a thread terminates its execution completely.
The stop() method is deprecated because it can leave shared resources in an inconsistent state.
Instead, use a shared flag to signal the thread to stop gracefully.
Output
Thread is running...
Thread is running...
Thread is running...
Stopping thread...
Thread stopped.
Obtain a thread's state
In Java, you can obtain a thread's state using the Thread class's getState() method.
This method returns a value from the Thread.State enum, which represents the
different states a thread can be in during its lifecycle.
Thread States
Java defines the following states for a thread:
1) NEW: A thread is in the NEW state after it has been created but before the start()
method has been invoked.
2) RUNNABLE: A thread enters the RUNNABLE state after the start() method has
been called. In this state, the thread is eligible for execution by the Java Virtual
Machine (JVM), but it may not be running at all times because other threads could
be executing. This is the state where a thread is actively running or waiting for a
CPU time slice.
3) BLOCKED: A thread enters the BLOCKED state when it is waiting for a monitor lock
to enter a synchronized block or method. When another thread is holding the
lock, the thread is blocked.
4) WAITING: A thread is in the WAITING state when it is waiting
indefinitely for another thread to perform a particular action. This is
typically the result of calling methods like Thread.sleep(), Object.wait(),
or LockSupport.park().
Output
MONDAY is a Weekday
TUESDAY is a Weekday
WEDNESDAY is a Weekday
THURSDAY is a Weekday
FRIDAY is a Weekday
SATURDAY is a Weekend
SUNDAY is a Weekend
Today is: MONDAY
Ordinal of SUNDAY: 6
5.Instantiation:Enumerations define a class type, but they are not
instantiated using the new keyword.
Enumeration variables are declared and used similarly to primitive
types.
6.Assignment and Comparison:
Enumeration variables can only hold values defined by the
enumeration.
Constants can be assigned to enumeration variables using the dot
notation( EnumType.Constant)
Constants can be compared for equality using the == operator
7.Switch Statements:Enumeration values can be used to control switch
statements.
All case statements within the switch must use constants from the
same enum as the switch expression.
Constants in case statements are referenced without qualification by
their enumeration type name.
values() and valueOf() methods
In Java, both the values() and valueOf() methods are built-in
methods available to all enum types.
These methods are automatically provided by the Java compiler
when you define an enum type.
1. values() Method
• The values() method is implicitly provided by Java for all enums.
This method returns an array of all the constants of the enum
type in the order they are declared.
OUTPUT: MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
2. valueOf(String name) Method
The valueOf() method is another built-in
method that is used to convert a String to its
corresponding enum constant.
OUTPUT:
Day is: MONDAY
Invalid day: No enum constant Day.FUNDAY