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,
-
Can you think of alternative ways, and how those might stack up against those discussed in the case? Break into small groups with your classmates to discuss the pros and cons of alternative...
-
Discuss the unique control problems created by EDI.
-
Wayward Airfreight, Inc. has asked you to recommend a new automatic parcel sorter. You have obtained the following bids: Using an MARR of 15% and a rate of return analysis, which alternative, if any,...
-
Question 3 Standard Costing and Sales Performance [20 marks] Suppose that you are the CEO of Fly-a-Kite Company, which manufactures a variety of kite kits. Lily, the assistant accountant of your...
-
On January 4, 2015, an FI has the following balance sheet (rates = 8 percent) DGAP = [8 (396/450)4] = 4.48 years > 0 The FI manager thinks rates will increase by 0.55 percent in the next three...
-
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...
-
A) Utilizing another case study project, prepare a work package for direct labor for a system of work such as concrete foundations, CIP concrete walls, tilt-up concrete walls, or SOG. Attach the...
-
Describe the importance of using family approaches in your work as a social worker. Discuss several client issues where you would recommend a family approach and why you think it would be beneficial.
-
2.Determine the angular momentum of the 20 kg disk or rod about point G and point O 2 m G 6 rad/s O No slipping
-
2) A 7%, 30-year bond has a par value of $1,000 and a call price of $1,030. It is callable in 10 years. The bond is currently selling for $1,060. Calculate the current yield, yield to maturity, and...
-
What is one of the benefits of the Madrid protocol in regard to protecting a firms intellectual property? a. It allows the extension of a firms patent into other member countries without the use of a...
-
Pacific Health Care Pacific Health Care ("PHC") operates 18 health care facilities in B.C. with more than 1 600 total beds, and it employs approximately 2 200 registered nurses ("RN") and 350...
-
To estimate proportions using bootstrapping methods, report successes as 1 and failures as 0. Then follow the same procedures that we used to estimate a mean using bootstrapping. Suppose a random...
-
Differentiate. y = ln(3x + 1) ln(5x + 1)
-
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...
-
Question 20 (1 point) Listen The Carters contributed $8,000 to the Path for Progress Political Action Committee What is the tax treatment of this contribution? O Deductible on Schedule E as an...
-
The Sheffield and Frizell Dental Clinic provides both preventive and orthodontic dental services. The two owners, Reese Sheffield and Anita Frizell, operate the clinic as two separate investment...
-
Montoure Company utiliza un sistema de inventario peridico. Realiz las siguientes transacciones de compra y venta en el ao calendario. Fecha Actividades Unidades Adquiridas al Costo Unidades vendidas...
Study smarter with the SolutionInn App