0% found this document useful (0 votes)
5 views9 pages

Understanding Multithreading Basics

Multithreading enables concurrent execution of multiple threads within a program, allowing for efficient use of shared memory and faster context-switching compared to multiprocessing. Threads can be created by extending the Thread class or implementing the Runnable interface, and they go through various states such as New, Runnable, Running, and Terminated. Examples of multithreading applications include handling multiple client requests in a server and performing background tasks in GUI applications.

Uploaded by

TRANAND TR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Understanding Multithreading Basics

Multithreading enables concurrent execution of multiple threads within a program, allowing for efficient use of shared memory and faster context-switching compared to multiprocessing. Threads can be created by extending the Thread class or implementing the Runnable interface, and they go through various states such as New, Runnable, Running, and Terminated. Examples of multithreading applications include handling multiple client requests in a server and performing background tasks in GUI applications.

Uploaded by

TRANAND TR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MULTITHREADING

 Multithreading allows concurrent execution of two or more parts of a


program. Each part is called a thread, and it is executed independently.
 Threads are light-weight processes within a process.
 Unlike in multiprocessing, threads use a shared memory area.
 Context-switching between the threads takes less time than process.
 Multithreading is particularly useful for tasks that are independent of
each other and can run simultaneously.
Examples:
 Handling multiple client requests in a server
 Performing background tasks in a GUI application

Life cycle of Thread

State Description
New Thread is created but not started
Runnable Ready to run; waiting for CPU
Running Actively executing instructions.
Blocked/Waiting Waiting for some other thread's action (e.g., I/O or
synchronization).
Timed Waiting Waiting for a specified period
Terminated The thread has finished execution

Thread Creation
Threads can be created by using two mechanisms:
1) Extending the Thread class
2) Implementing the Runnable Interface
By extending the Thread class
class TicketBooking
{
private int availableSeats = 5;

// Synchronized method to book a ticket


public synchronized void bookSeat(String customerName, int
numberOfSeats)
{
[Link](customerName + " is trying to book " +
numberOfSeats + " seat(s).");

// Check if seats are available


if (numberOfSeats <= availableSeats)
{
[Link]("Seats available. Processing booking
for " + customerName + "...");
try
{
[Link](2000); // Simulate booking process time
}
catch (InterruptedException e)
{
[Link]([Link]());
}
// Update the available
seats availableSeats -=
numberOfSeats;
[Link](customerName + " successfully booked " +
numberOfSeats + " seat(s).");
} [Link]("Seats remaining: " + availableSeats);
els
e
{
[Link]("Sorry, not enough seats available for " +
customerName + ".");
}
}
}

// User Thread trying


to book seats class
Customer extends
Thread {
TicketBooking
bookingSystem;
String
customerName;
int seatsToBook;

// Constructor to initialize booking system and booking details


public Customer(TicketBooking bookingSystem, String customerName, int
seatsToBook) { [Link] = bookingSystem;
[Link] =
customerName;
[Link] =
seatsToBook;
}
// Thread's run method
for booking seat public
void run() {
[Link](customerName, seatsToBook);
}
}

// Main Class
public class
OnlineTicketBookingS
ystem { public static
void main(String[]
args) {
// Shared TicketBooking system
TicketBooking bookingSystem = new TicketBooking();

// Creating multiple Customer threads


Customer customer1 = new
Customer(bookingSystem, "Alice", 2); Customer
customer2 = new Customer(bookingSystem, "Bob",
4); Customer customer3 = new
Customer(bookingSystem, "Charlie", 1);

// Starting the
threads
[Link]()
;
[Link]()
;
[Link]()
;
}
}
By implementing the Runnable interface
The Runnable interface has only one method: run(). This method must be
implemented to define the task that will be executed by a thread.

Syntax:
public interface Runnable
{
public void run();
}
// Class implementing the
Runnable interface class
MyRunnable implements
Runnable
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
[Link](i + " from " +
[Link]().getName()); try
{
[Link](1000); // Pause for 1 second
}
catch (InterruptedException e)
{
[Link](e);
}
}
}
}

public class TestRunnable


{
public static void main(String[] args)
{
// Create an instance of the class that
implements Runnable MyRunnable
myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new
MyRunnable();
// Create Thread objects, passing the Runnable objects to the Thread
constructor Thread t1 = new Thread(myRunnable1);
Thread t2 = new Thread(myRunnable2);
// Start
the
threads
[Link]()
;
[Link]();
}
}

You might also like