0% found this document useful (0 votes)
17 views

Java 10

Program in java to implement interface

Uploaded by

aqsakhatun7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java 10

Program in java to implement interface

Uploaded by

aqsakhatun7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aqsa 22DCS008

PRACTICAL NO 10
Name of practical: Write a program in Java to implement multithreading.

Runnable interface and Thread class

In Java, both the Runnable interface and the Thread class are used to create and manage
threads. The Runnable interface is a functional interface that is implemented by a class whose
instances are intended to be executed by a thread. It contains a single abstract method called
run() which needs to be overridden.
The Thread class itself represents a thread of execution. It provides constructors and methods to
create and manage threads, including the ability to start a thread, stop it, and perform operations
on it. It also implements the Runnable interface.

Why Runnable interface preferred over thread class?

The Runnable interface is preferred over extending the Thread class for these reasons:

1. Separation of concerns: It separates the task (what to execute) from the thread (how to
execute it), promoting cleaner design.
2. Single inheritance: Java allows a class to extend only one class. By using Runnable,
you can extend another class if needed.
3. Better reusability: You can reuse the same Runnable task across multiple threads,
whereas a Thread instance is tied to a single task.
4. Thread pools: Runnable is better suited for use with thread pools, which efficiently
manage multiple threads.
5. Encourages composition: Runnable favors composition over inheritance, which is a
good object-oriented design principle. Program

class BankAccount {
private int balance = 2000; // Initial balance

// Synchronized method for depositing money


public synchronized void deposit(int amount) {
System.out.println(Thread.currentThread().getName() + " is trying to deposit: " + amount);
balance += amount;
System.out.println(Thread.currentThread().getName() + " successfully deposited. New
Balance: " + balance);
}

// Synchronized method for withdrawing money


public synchronized void withdraw(int amount) {
if (amount <= balance) {
System.out.println(Thread.currentThread().getName() + " is trying to withdraw: " +
amount);

Practical no: 10
Aqsa 22DCS008

balance -= amount;
System.out.println(Thread.currentThread().getName() + " successfully withdrew. New
Balance: " + balance);
} else {
System.out.println(Thread.currentThread().getName() + " attempted to withdraw: " +
amount + " but insufficient funds!");
}
}

// Getter method for balance public


synchronized int getBalance() {
return balance;
}
}

// Class extending Thread representing a customer depositing or withdrawing money class


CustomerThread extends Thread {
BankAccount account;
int amount;
boolean isDeposit;

// Constructor to initialize the account, amount, and operation type public


CustomerThread(BankAccount account, int amount, boolean isDeposit)
{ this.account = account; this.amount = amount;
this.isDeposit = isDeposit;
}

// Overriding the run method to either deposit or withdraw money


public void run() { if (isDeposit) {
account.deposit(amount);
} else
{ account.withdraw(amount);
}
}
}

public class New {


public static void main(String[] args) throws InterruptedException {
// Creating a shared BankAccount object
BankAccount account = new BankAccount();

// Creating customer threads for depositing and withdrawing


CustomerThread customer1 = new CustomerThread(account, 900, true); // Deposit
CustomerThread customer2 = new CustomerThread(account, 1400, false); // Withdraw
CustomerThread customer3 = new CustomerThread(account, 700, true); // Deposit

Practical no: 10
Aqsa 22DCS008

CustomerThread customer4 = new CustomerThread(account, 2200, false); // Withdraw

// Starting all the customer threads sequentially


customer1.start();
customer1.join(); // Wait for customer1 to finish

customer2.start();
customer2.join(); // Wait for customer2 to finish

customer3.start();
customer3.join(); // Wait for customer3 to finish

customer4.start();
customer4.join(); // Wait for customer4 to finish

// Using the getter method to display the final balance


System.out.println("All transactions completed. Final Balance: " + account.getBalance());
}}

Output

Practical no: 10

You might also like