Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary For student

Rating
-
Sold
-
Pages
29
Uploaded on
09-06-2025
Written in
2024/2025

It helps for beginners and advanced level

Institution
Course

Content preview

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) {

Written for

Institution
Course

Document information

Uploaded on
June 9, 2025
Number of pages
29
Written in
2024/2025
Type
SUMMARY

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
Kusuma

Get to know the seller

Seller avatar
Kusuma Mandavya first grade college
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
10 months
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions