Question: Rewrite Listing 30.6, ThreadCooperation.java, using the object?s wait() and notifyAll() methods. Listing 1 import java.util.concurrent.*; 2 import java.util.concurrent.locks.*; 3 4 public class ThreadCooperation { private

Rewrite Listing 30.6, ThreadCooperation.java, using the object?s wait() and notifyAll() methods.

Listing

1 import java.util.concurrent.*; 2 import java.util.concurrent.locks.*; 3 4 public class ThreadCooperation {

private static Account account - new Account(O; public static void main(String[] args)

1 import java.util.concurrent.*; 2 import java.util.concurrent.locks.*; 3 4 public class ThreadCooperation { private static Account account - new Account(O; 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 (); 9. 10 11 12 13 14 15 System.out.printIn("Thread 1\t\tThread 2\t\tBalance"); 16 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) { 17 18 19 20 21 account.deposit((int)(Math.random() * 10) + 1); Thread.sleep(1000); 22 23 24 25 catch (InterruptedException ex) { ex.printStackTrace(); 26 27 28 29 30 31 public static class WithdrawTask implements Runnable { @Override // Keep subtracting an amount from the account public void run() { while (true) { account.withdrawCCint) (Math.random() * 10) + 1); 32 33 34 35 36 37 38 39 40 // An inner class for account 42 private static class Account { // Create a new lock 43 = new ReentrantLock(); 44 private static Lock lock 45 // Create a condition private static Condition newDeposit = lock.newCondition(); 46 47 48 49 50 private int balance = 0; 51 public int getBalance() { return balance; 52 53 54 55 public void withdraw(int amount) { lock.lock(); // Acquire the lock try { while (balance < amount) { 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 72 73 74 75 76 77 78 79 80 81 82 83 System.out.print1n("\t\t\tWait for a deposit"); newDeposit.await(); balance -- amount; System.out.println("\t\t\tWithdraw " + amount + "\t\t" + 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 + "\t\t\t\t\t" + getBalance()); // Signal thread waiting on the condition newDeposit.signa1A11(); 84 finally { lock.unlock () ; // Release the lock 85 86 87 88 89 90 }

Step by Step Solution

3.45 Rating (165 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program plan Create a new inner class named Account to model the account with two methods depositint and withdrawint The class named DepositTask to ad... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!