0% found this document useful (0 votes)
32 views12 pages

Java Multithreading: Creation & Benefits

The document discusses the concept of threads and multithreading in Java, highlighting their importance for efficient CPU utilization and program performance. It explains two methods for creating threads: by extending the Thread class and by implementing the Runnable interface, along with a real-life example of a synchronized bank account application. The conclusion emphasizes that Java supports multithreading and recommends using the Runnable approach for better efficiency.

Uploaded by

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

Java Multithreading: Creation & Benefits

The document discusses the concept of threads and multithreading in Java, highlighting their importance for efficient CPU utilization and program performance. It explains two methods for creating threads: by extending the Thread class and by implementing the Runnable interface, along with a real-life example of a synchronized bank account application. The conclusion emphasizes that Java supports multithreading and recommends using the Runnable approach for better efficiency.

Uploaded by

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

Creating Thread and Creating Multiple

Threads in Java

Presented by: DEEPAK N ADUR


USN: 2KA24CS016
THREAD
● A thread is a lightweight sub-process

● It is the smallest unit of CPU execution

● Multiple threads share same memory space

“According to Java, a thread is a lightweight sub-process and the smallest unit of


execution that shares memory with other threads.”
Multithreading

● Multithreading allows concurrent execution

● Improves CPU utilization

● Increases program efficiency

“Multithreading is the ability of a Java program to execute two or more threads


simultaneously for better performance.”
Need for Multithreading

● Efficient CPU usage

● Faster execution

● Better responsiveness

● Used in server and real-time systems

“Multithreading improves system performance by utilizing CPU resources efficiently


and is commonly used in real-time and server applications.”
Ways to Create Thread in Java

1. By extending Thread class

2. By implementing Runnable interface

“Java provides two standard methods to create threads: extending the


Thread class and implementing the Runnable interface.”
Program – Thread Class
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
public static void main(String args[]) {
MyThread t = new MyThread();
[Link]();
}
}

“Here, the start method creates a new thread and internally calls the run method.”
Real-Life Multithreading Code
{Bank Account with Synchronization}
class BankAccount {

private int balance = 10000;

synchronized void withdraw(int amount) {

[Link]([Link]().getName()
+ " wants to withdraw Rs." + amount);

if (balance >= amount) {


balance = balance - amount;
[Link]("Withdrawal successful");
[Link]("Remaining balance = Rs." + balance);
} else {
[Link]("Insufficient balance");
}

[Link]("--------------------------------");
}
}

class Customer implements Runnable {

BankAccount account;
int amount;

Customer(BankAccount account, int amount) {


[Link] = account;
[Link] = amount;
}

public void run() {


[Link](amount);
}
}

public class Main {


public static void main(String[] args) {

BankAccount account = new BankAccount();

Thread t1 = new Thread(new Customer(account, 4000), "Customer-1");


Thread t2 = new Thread(new Customer(account, 5000), "Customer-2");
Thread t3 = new Thread(new Customer(account, 3000), "Customer-3");

[Link]();
[Link]();
[Link]();
}
}
OUTPUT
Customer-1 wants to withdraw Rs.4000
Withdrawal successful
Remaining balance = Rs.6000
--------------------------------
Customer-2 wants to withdraw Rs.5000
Withdrawal successful
Remaining balance = Rs.1000
--------------------------------
Customer-3 wants to withdraw Rs.3000
Insufficient balance
--------------------------------
Applications

● Operating systems

● Web servers

● Banking systems

● Games

“Multithreading is used in applications that require parallel execution.”


Conclusion

● Java supports multithreading

● Threads improve efficiency

● Runnable approach is preferred

“In conclusion, Java provides efficient multithreading support, and implementing Runnable is
the recommended approach.”

You might also like