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: 70% (10 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,
-
The top food snacks consumed by adults aged 1854 are gum, chocolate candy, fresh fruit, potato chips, breath mints/candy, ice cream, nuts, cookies, bars, yogurt, and crackers. Out of a random sample...
-
5 percent interest per month. The sterilizer was delivered on August 23, 1996. The last payment was received on April 21, 1998. At the time of the last payment, the defendant still owed the plaintiff...
-
Data for Mariner Designs, Inc., follow: Requirements 1. Prepare a horizontal analysis of the comparative income statement of Mariner Designs, Inc. Round percentage changes to one decimal place. 2....
-
Netflix estimates that North American demand for its streaming service has an elasticity of approximately -1.5. In 2022, 74 million subscribers generated revenues of just over $14 billion at an...
-
Spritz Company owns 15% of the stock of Turner Corporation. The investment was purchased for $200,000. At the beginning of the current year, the investment had a fair value of $230,000. At the end of...
-
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...
-
The manager of a chain of three discount drug stores wishes to investigate the level of discount coupon redemption at its stores. All three stores have the same sales volume. Therefore, the manager...
-
1. In Plato's Symposium, Speech of Aristophanes, the character Aristophanes argues that erotic love arises from a universal human feeling of lack, need, or incompleteness. Based on their letters to...
-
A projectile is fired in Earth's gravitational field (g = 9.80 m/s2) with a horizontal velocity of v = 8.25 m/s. How far does the projectile fall in the vertical direction in 0.631 s? [Round your...
-
Give a positive or negative example of a personal experience with packaging. Looking at that package, briefly describe how you see the ten material handling principles had a positive or negative...
-
In this task you are required to survey the team to identify common workplace stressors amongst educators. * Your plan is to conduct a survey and then implement strategies to address the issues you
-
What do we think is the source of gold and other elements that require a neutron-rich environment for their formation?
-
A plastic beach ball has radius 20.0 cm and mass 0.10 kg, not including the air inside. (a) What is the weight of the beach ball including the air inside? Assume the air density is 1.3 kg/m 3 both...
-
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...
-
The following PIC code displays a countdown with a 1-second interval, in the spirit of Prac 1. start: call LCD_init movlw d'100' movwf count again: call LCD clear movf count, w call LCD BCD call...
-
A company is developing a special vehicle for Arctic exploration. The development requires an initial investment of $65,000 and investments of $53,000 and $42,000 for the next two years,...
-
what ways does the integration of literary theory and critical discourse into the fabric of the narrative contribute to a meta-commentary on the nature of literature and its relationship to society,...
Study smarter with the SolutionInn App