JAVA PROGRAMMING: Write a program that extends the ThreadCooperation program as
ID: 3683145 • Letter: J
Question
JAVA PROGRAMMING:
Write a program that extends the ThreadCooperation program as shown below by:
1. having multiple depositor and withdrawer threads. Your program should take two command line parameters: the first is an int for the number of depositor threads to create. The second is an int for the number of withdrawer threads to create.
2. have depositor threads block if the deposit would result in a balance of more than $9999 dollars.
Hint: Thread.sleep IS NOT a synchronization mechanism.
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class ThreadCooperation {
private static Account account = new Account();
public static void main(String[] args) {
// Create a thread pool with two threads
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.execute(new DepositTask());
executor.execute(new WithdrawTask());
executor.shutdown();
System.out.println("Thread 1 Thread 2 Balance");
}
public static class DepositTask implements Runnable {
@Override // Keep adding an amount to the account
public void run() {
try { // Purposely delay it to let the withdraw method proceed
while (true) {
account.deposit((int)(Math.random() * 10) + 1);
Thread.sleep(1000);
}
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static class WithdrawTask implements Runnable {
@Override // Keep subtracting an amount from the account
public void run() {
while (true) {
account.withdraw((int)(Math.random() * 10) + 1);
}
}
}
// An inner class for account
private static class Account {
// Create a new lock
private static Lock lock = new ReentrantLock();
// Create a condition
private static Condition newDeposit = lock.newCondition();
private int balance = 0;
public int getBalance() {
return balance;
}
public void withdraw(int amount) {
lock.lock(); // Acquire the lock
try {
while (balance < amount) {
create two threads
create a lock
create a condition
acquire the lock
System.out.println(" Wait for a deposit");
newDeposit.await();
}
balance -= amount;
System.out.println(" Withdraw " + amount +
" " + getBalance());
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
finally {
lock.unlock(); // Release the lock
}
}
public void deposit(int amount) {
lock.lock(); // Acquire the lock
try {
balance += amount;
System.out.println("Deposit " + amount +
" " + getBalance());
// Signal thread waiting on the condition
newDeposit.signalAll();
}
finally {
lock.unlock(); // Release the lock
}
}
}
}
Explanation / Answer
package assignment;
import java.util.Scanner;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class ThreadCooperation {
private static Account account = new Account();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(args != null && args.length ==2) {
// Create a thread pool with two threads
int nDepositThr = Integer.parseInt(args[0]);
int nWithdrawThr = Integer.parseInt(args[1]);
System.out.println(nDepositThr+" -- "+nWithdrawThr);
ExecutorService executor = Executors.newFixedThreadPool(2);
for(int i = 0; i < nDepositThr; i++)
executor.execute(new DepositTask());
for(int i = 0; i < nWithdrawThr; i++)
executor.execute(new WithdrawTask());
executor.shutdown();
System.out.println("Thread 1 Thread 2 Balance");
} else
System.out.println(" Need command line arguments");
}
public static class DepositTask implements Runnable {
@Override
// Keep adding an amount to the account
public void run() {
try { // Purposely delay it to let the withdraw method proceed
while (true) {
account.deposit((int) (Math.random() * 10) + 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static class WithdrawTask implements Runnable {
@Override
// Keep subtracting an amount from the account
public void run() {
while (true) {
account.withdraw((int) (Math.random() * 10) + 1);
}
}
}
// An inner class for account
private static class Account {
// Create a new lock
private static Lock lock = new ReentrantLock();
// Create a condition
private static Condition newDeposit = lock.newCondition();
private int balance = 0;
public int getBalance() {
return balance;
}
public void withdraw(int amount) {
lock.lock(); // Acquire the lock
try {
while (balance < amount) {
// create two threads create a lock create a condition
// acquire the lock
System.out.println(" Wait for a deposit");
newDeposit.await();
}
balance -= amount;
newDeposit.signalAll();
System.out.println(" Withdraw " + amount + " "
+ getBalance());
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock(); // Release the lock
}
}
public void deposit(int amount) {
lock.lock(); // Acquire the lock
try {
if(balance + amount > 9999)
newDeposit.await();
balance += amount;
System.out.println("Deposit " + amount + " "
+ getBalance());
// Signal thread waiting on the condition
newDeposit.signalAll();
}catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock(); // Release the lock
}
}
}
}
2 -- 3
Thread 1 Thread 2 Balance
Deposit 9 9
Deposit 2 11
Deposit 6 17
Deposit 5 22
Deposit 10 32
Deposit 10 42
Deposit 7 49
Deposit 3 52
Deposit 3 55
Deposit 1 56
Deposit 8 64
Deposit 9 73
Deposit 8 81
Deposit 8 89
Deposit 3 92
Deposit 9 101
Deposit 2 103
Deposit 5 108
Deposit 5 113
Deposit 9 122
Deposit 1 123
Deposit 1 124
Deposit 4 128
Deposit 4 132
Deposit 6 138
Deposit 1 139
Deposit 10 149
Deposit 6 155
Deposit 6 161
Deposit 10 171
Deposit 1 172
Deposit 2 174
Deposit 9 183
Deposit 5 188
Deposit 8 196
Deposit 1 197
Deposit 6 203
Deposit 9 212
Deposit 9 221
Deposit 1 222
Deposit 5 227
Deposit 3 230
Deposit 5 235
Deposit 5 240
Deposit 3 243
Deposit 3 246
Deposit 6 252
Deposit 8 260
Deposit 10 270
Deposit 5 275
Deposit 8 283
Deposit 7 290
Deposit 5 295
Deposit 3 298
Deposit 10 308
Deposit 9 317
Deposit 10 327
Deposit 6 333
Deposit 8 341
Deposit 2 343
Deposit 3 346
Deposit 3 349
Deposit 9 358
Deposit 10 368
Deposit 8 376
Deposit 3 379
Deposit 9 388
Deposit 1 389
Deposit 10 399
Deposit 3 402
Deposit 4 406
Deposit 6 412
Deposit 8 420
Deposit 3 423
Deposit 4 427
Deposit 4 431
Deposit 3 434
Deposit 6 440
Deposit 5 445
Deposit 6 451
Deposit 2 453
Deposit 9 462
Deposit 8 470
Deposit 3 473
Deposit 7 480
Deposit 1 481
Deposit 3 484
Deposit 10 494
Deposit 1 495
Deposit 8 503
Deposit 3 506
Deposit 9 515
Deposit 9 524
Deposit 10 534
Deposit 10 544
Deposit 3 547
Deposit 2 549
Deposit 3 552
Deposit 6 558
Deposit 8 566
Deposit 4 570
Deposit 7 577
Deposit 9 586
Deposit 2 588
Deposit 5 593
Deposit 4 597
Deposit 3 600
Deposit 3 603
Deposit 10 613
Deposit 3 616
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.