Multithreading
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program
for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-
weight processes within a process.
LIFE CYCLE OF A THREAD
During the lifetime of a thread, there are many states it can enter. They include:
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
1.Newborn State:
When we create a thread object, the thread is born and is said to be in newborn state. The
thread is not yet scheduled for running. At this state, we can do only one of the
following things with it.
•Schedule it for running using start( ) method.
•Kill it using stop() method.
2.Runnable State:
This runnable state means that the thread is ready for execution and is waiting for the
availability of the processor. That is, the thread has joined the queue of threads that are waiting
for execution. If all threads have equal priority, then they are given time slots for execution in
round robin fashion. I.e., first-come, first – serve manner. The thread that relinquishes control
joins the queue at the end and again waits for its turn. This process of assigning time to threads
is known as time-slicing.
Running means that the processor has given its time to the thread for its execution. The thread
runs until it relinquishes control on its own or it is pre-empted by a higher priority thread.
1. It has been suspended using suspend ( ) method. A suspended thread can be revived by
using the resume ( ) method.
2. It has been made to sleep, we can put a thread to sleep for a specified time period using the
, method sleep (time) where time is in milliseconds. This means that the thread is out of the
queue during this time period.
3. It has been told to wait until some even to occurs. This is done using the wait()
methods. The thread can be scheduled to run again using the notify() method.
3.Blocked State
A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently by
running state. This happens when the thread is suspended, sleeping or waiting in order to satisfy certain
requirements. A blocked thread is considered “not runnable ”but not dead and therefore fully qualified to run
again.
4.Dead State
Every thread has life cycle. A running thread ends its life when it has completed executing its run() method.
A thread can be killed as soon it is born, or while it is running or even when it is in “not runnable”(blocked)
condition
Thread Class
In Java, the Thread class is fundamental for creating and managing concurrent execution paths within a
program. It provides the mechanism to run multiple blocks of code seemingly simultaneously. Each thread
represents an independent flow of control.
Creating Threads
There are two primary ways to create a thread in Java:
1. Extending the Thread class
2. Implementing the Runnable interface
Extending the Thread class:
o Define a new class that inherits from java.lang.Thread.
o Override the run() method to define the task performed by the thread.
o Create an instance of the new class and call the start() method to begin execution.
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running");
}
}
MyThread thread = new MyThread();
thread.start();
Implementing the Runnable interface:
o Create a class that implements the java.lang.Runnable interface.
o Provide the implementation for the run() method.
, o Instantiate a Thread object, passing an instance of the Runnable class to its constructor.
o Call the start() method of the Thread object.
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread is running");
}
}
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
Thread Methods
The Thread class offers several methods for controlling thread behavior, including:
start(): Initiates the thread's execution.
run(): Contains the code executed by the thread.
join(): Waits for the thread to complete its execution.
sleep(): Pauses the thread for a specified duration.
interrupt(): Interrupts the thread, potentially causing it to terminate.
isAlive(): Checks if the thread is currently running.
getName(): Returns the name of the thread.
setName(): Sets the name of the thread.
setPriority(): Sets the priority of the thread.
getPriority(): Gets the priority of the thread.
currentThread(): Returns a reference to the currently executing thread.
Stop(): To kill the Thread.
wait(): Waiting stage of Thread until the condition.
Notify():-After the condition the waiting Thread automatically execute.
Resume(): Resumes the execution of the Thread.
Suspend(): It is similar to wait when using suspend the method resume will be taken
Yield(): It is used to stop the execution of a particular Thread
EXAMPLE PROGRAM
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
System.out.println("Thread: " + getName() + ", Count: " + i);
} catch (InterruptedException e) {
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program
for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-
weight processes within a process.
LIFE CYCLE OF A THREAD
During the lifetime of a thread, there are many states it can enter. They include:
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
1.Newborn State:
When we create a thread object, the thread is born and is said to be in newborn state. The
thread is not yet scheduled for running. At this state, we can do only one of the
following things with it.
•Schedule it for running using start( ) method.
•Kill it using stop() method.
2.Runnable State:
This runnable state means that the thread is ready for execution and is waiting for the
availability of the processor. That is, the thread has joined the queue of threads that are waiting
for execution. If all threads have equal priority, then they are given time slots for execution in
round robin fashion. I.e., first-come, first – serve manner. The thread that relinquishes control
joins the queue at the end and again waits for its turn. This process of assigning time to threads
is known as time-slicing.
Running means that the processor has given its time to the thread for its execution. The thread
runs until it relinquishes control on its own or it is pre-empted by a higher priority thread.
1. It has been suspended using suspend ( ) method. A suspended thread can be revived by
using the resume ( ) method.
2. It has been made to sleep, we can put a thread to sleep for a specified time period using the
, method sleep (time) where time is in milliseconds. This means that the thread is out of the
queue during this time period.
3. It has been told to wait until some even to occurs. This is done using the wait()
methods. The thread can be scheduled to run again using the notify() method.
3.Blocked State
A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently by
running state. This happens when the thread is suspended, sleeping or waiting in order to satisfy certain
requirements. A blocked thread is considered “not runnable ”but not dead and therefore fully qualified to run
again.
4.Dead State
Every thread has life cycle. A running thread ends its life when it has completed executing its run() method.
A thread can be killed as soon it is born, or while it is running or even when it is in “not runnable”(blocked)
condition
Thread Class
In Java, the Thread class is fundamental for creating and managing concurrent execution paths within a
program. It provides the mechanism to run multiple blocks of code seemingly simultaneously. Each thread
represents an independent flow of control.
Creating Threads
There are two primary ways to create a thread in Java:
1. Extending the Thread class
2. Implementing the Runnable interface
Extending the Thread class:
o Define a new class that inherits from java.lang.Thread.
o Override the run() method to define the task performed by the thread.
o Create an instance of the new class and call the start() method to begin execution.
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread is running");
}
}
MyThread thread = new MyThread();
thread.start();
Implementing the Runnable interface:
o Create a class that implements the java.lang.Runnable interface.
o Provide the implementation for the run() method.
, o Instantiate a Thread object, passing an instance of the Runnable class to its constructor.
o Call the start() method of the Thread object.
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread is running");
}
}
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
Thread Methods
The Thread class offers several methods for controlling thread behavior, including:
start(): Initiates the thread's execution.
run(): Contains the code executed by the thread.
join(): Waits for the thread to complete its execution.
sleep(): Pauses the thread for a specified duration.
interrupt(): Interrupts the thread, potentially causing it to terminate.
isAlive(): Checks if the thread is currently running.
getName(): Returns the name of the thread.
setName(): Sets the name of the thread.
setPriority(): Sets the priority of the thread.
getPriority(): Gets the priority of the thread.
currentThread(): Returns a reference to the currently executing thread.
Stop(): To kill the Thread.
wait(): Waiting stage of Thread until the condition.
Notify():-After the condition the waiting Thread automatically execute.
Resume(): Resumes the execution of the Thread.
Suspend(): It is similar to wait when using suspend the method resume will be taken
Yield(): It is used to stop the execution of a particular Thread
EXAMPLE PROGRAM
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
System.out.println("Thread: " + getName() + ", Count: " + i);
} catch (InterruptedException e) {