Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***** JAVA expert please help me out. Write a program that expands on the Thread

ID: 3820275 • Letter: #

Question

*****

JAVA expert please help me out.

Write a program that expands on the ThreadCooperation program

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

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

Explanation / Answer

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) {
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
}
}
}
}