Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***** JAVA expert please help me out. Write a program that expands on the ThreadCooperation program --------------- ThreadCooperation.java --------------- import java.util.concurrent.*; import java.util.concurrent.locks.*; public class

*****

JAVA expert please help me out.

Write a program that expands on the ThreadCooperation program

image text in transcribed

---------------ThreadCooperation.java---------------

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\t\tThread 2\t\tBalance"); } 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 out.println("\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.signalAll(); } finally { lock.unlock(); // Release the lock } } } } 
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. Note that the thread pool will have to be expanded to cover the user-specified number of threads. 2. have depositor threads block if the deposit would result in a balance of more than $49 dollars 3. eliminate all calls to Thread.sleep (except for debugging) 4. let your threads run forever, this isn't the usual way we write our programs, but in this case Hint: 1. Thread. sleep IS NOT a synchronization mechanism 2. For the "count words in a directory" program, the hardest part (by far) is figuring out the total number of words in all the files. A worker thread has the information for its one file, but all those numbers have to be combined somewhere someway to be printed. That means that the workers have to have finished counting words before the total can be calculated completely. And whatever code is actually printing that total number knows (for certain) that all the workers have completed their task. And it means that there's a central place for the information to reside or be calculated. There are several approaches that will work, but remember that I once told a 251 class that "static data is evil," so don't do that 3. A good test for cooperating threads is to, in turn, make one thread "slow" and its parter "fast." For example, to one test would be to change line 24 o Thread sleep(1) This would test that the producer test ConsumerProducer of listing 30.7, could suspend frequently, and that the consumer "woke up" sleeping producers. A second test would involve restoring line 24 and changing line 40 to Thread.Sleep(1), This would test that the consumer could suspend frequently, and that the producer "woke up" sleeping consumers. Thread.sleep IS NOT a synchronization mechanism, but it can be a useful debugging tool

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions