Rewrite Listing 32.6, ThreadCooperation.java, using the objects wait() and notifyAll() methods. Data from Listing 32.6, 1mport java.util.concurrent.:
Question:
Rewrite Listing 32.6, ThreadCooperation.java, using the object’s wait() and notifyAll() methods.
Data from Listing 32.6,
Transcribed Image Text:
1mport java.util.concurrent.: 2 1mport java.util.concurrent. locks.: 1 3 4 publ1c class ThreadCooperation { private static Account account = new Account (): 5 6 public static void main(String[] args) { II Create a thread pool with two threads ExecutorService executor = Executors.newFixedThreadPool(2): executor.execute(new DepositTask ()): executor.execute(new WithdrawTask ()): executor.shut down (): 7 8 9 10 11 12 13 System.out.printin("Thread 11titThread 21ti tBalance"): 14 15 16 17 public static class DepositTask 1mplements Runnable { e0verride // Keep adding an amount to the account publ1c void run () { try { // Purposely delay it to let the wi thdraw method proceed wh1le (true) { account.deposit((1nt) (Math.random ()· 10) + 1): Thread.sleep(1000) : 18 19 20 21 catch (InterruptedException ex) { ex . printStackTrace (): 31 publ1c static class Withdraw Task 1mpiements Runnable { e0verride // Keep subtracting an amount from the account publ1c void run () { wh1le (true) ( account.withdraw ( (1nt) (Math.random()· 10) + 1); } 32 33 34 35 36 37 38 39 40 I| An inner class for account private static class Account { II Create a new lock private static Lock lock = neN ReentrantLock (): 41 42 43 44 45 /I Create a condition private static Condition newDeposit = lock.newCondition(): 46 47 48 private int balance = 0: 49 50 public int getBalance () { return balance; 51 52 53 54 pub11c void withdraw (1nt amount) { lock. lock (): /I Acquire the lock try { while (balance < amount) { 55 56 57 58
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 69% (13 reviews)
import javautilconcurrent public class Exercise3208 private static Account account new Account publi...View the full answer
Answered By
Nazrin Ziad
I am a post graduate in Zoology with specialization in Entomology.I also have a Bachelor degree in Education.I posess more than 10 years of teaching as well as tutoring experience.I have done a project on histopathological analysis on alcohol treated liver of Albino Mice.
I can deal with every field under Biology from basic to advanced level.I can also guide you for your project works related to biological subjects other than tutoring.You can also seek my help for cracking competitive exams with biology as one of the subjects.
3.30+
2+ Reviews
10+ Question Solved
Related Book For
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Rewrite Listing 21.7 CountKeywords. java to read in a Java source code file and count the occurrence of each keyword in the file, but don?t count the keyword if it is in a comment or in a string...
-
Using the data from Exercise 6 "Bear Measurements," the linear correlation coefficient is r = 0.980. Data From Exercise 6 Use Table 2-11 on page 71 to find the critical values of r. Based on a...
-
Rewrite Exercise 37.13 using JSP. Data from Exercise 37.13 Write a Java servlet for Exercise 33.1, as shown in Figure 37.34. Exercse37_13- Mozilo Firefer x Ee E yew Hetory Bookmaks Iook Heb Exoics_13...
-
Gavin wrote the equation p=(3(s+100))/(4) to represent p, the profit he makes from s sales in his lawn -mowing business. Which equation is solved for s?
-
What is the constant k that makes the following function a valid density? fx (x) = ( 0 otherwise,
-
12. On December 31, 2021, a customer prepaid the Company for consulting services to be performed during February 2022. The prepayment was recorded as debit to Cash for $30,000 and a credit to...
-
What do you think of this? Should corporate blogs and other forms of social media be allowed? Why or why not?
-
A large metropolitan health-care complex, General Hospital, has had difficulty controlling its accounts receivable. Costs currently available from the information system are inaccurate and have led...
-
List in a bullet format the key items that should be documented in an audit engagement letter. Is an engagement letter a legally binding contract? Explain.
-
[5] A marksman is firing a pistol at a 5 cm diameter, circular bullseye some distance away. The center of the target is considered (0, 0). This marksman pulls a little to the right (positive x) and a...
-
Revise Listing 32.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows: public static > void parallelMergeSort(E[] list) Data from Listing 32.10, 1mport...
-
Rewrite Programming Exercise 15.32 using a thread to control the clock animation. Data from Programming Exercise 15.32 Modify Listing 14.21, ClockPane.java, to add the animation into this class and...
-
Suppose that the domestic demand for television sets is described by Q = 40,000 180P and that the supply is given by Q = 20P. Televisions can currently be freely imported at the world price of $160....
-
How do multi-track diplomacy frameworks, integrating official, unofficial, and grassroots efforts at different levels of society, enhance the effectiveness and inclusivity of conflict resolution...
-
As explained by Welch, what should managers do to determine what their own organizations have been up to ?
-
As an administrator how do you demonstrate below situation with suitable examples. 1 Completes tasks to a high standard 2 Demonstrates the necessary level of expertise required to complete tasks and...
-
What influences do the pharmaceutical companies have on psychiatry? What acronym can guide you in formulating a treatment plan (hint: Your instructor emphasizes this when creating a treatment plan,...
-
How do you write a board paper from an article? for example how would y a board paper from the article below look like? Aritcle...
-
The following data relate to material J for last month: Using the LIFO valuation method, what was the value of the closing stock for last month?(a) ?3300(b) ?3500(c) ?3700(d) ?3900 Opening stock...
-
Find the equations of the ellipses satisfying the given conditions. The center of each is at the origin. Passes through (2, 2) and (1, 4)
-
Programming Exercise 24.12 retrieves the last 100 prime numbers from a file PrimeNumbers.dat. Write a client program that requests the server to send the last 100 prime numbers in an ArrayList. Name...
-
Write a program that enables two users to chat. Implement one user as the server (Figure 31.21a) and the other as the client (Figure 31.21b). The server has two text areas: one for entering text and...
-
Write a program that displays the number of students in each department in a pie chart and a bar chart, as shown in Figure 32.27b. The number of students for each department can be obtained from the...
-
Relate PSA (Public Securities Association) speed to the average life of a MBS. Describe the PSA measure and discuss which MBS would have the greater average life, one with a PSA of 100 or one with a...
-
Which of the following statement about swaps is least accurate? A. In a plain vanilla interest rate swap, the notional principal is swapped. B. The default problem [i.e. default risk] is the most...
-
7. How much would be in your savings account in nine years after depositing $340 today if the bank pays 9 percent per year? (Do not round intermediate calculations. Round your answer to 2 decimal...
Study smarter with the SolutionInn App