CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Denis
Level 22
Kharkiv
  • 28.06.2019
  • 1171views
  • 1comment

help me pls

Question about the task Pharmacy
Java Core,  Level 7,  Lesson 10
Archived

Implement the Runnable interface in the Pharmacy and Person classes.
All threads should run until isStopped is true.
Here's the logic for the Pharmacy class: drugController should make a random sale (call the void sell(Drug drug, int count) method) of a random drug (getRandomDrug) in a random amount (getRandomCount) and wait 300 ms.
Here's the logic for the Person class: drugController should make a random purchase (call the void buy(Drug drug, int count) method) of a random drug (getRandomDrug) in a random amount (getRandomCount) and wait 100 ms.
Put the synchronized keyword where necessary.

Requirements:
  • The Solution class must have a public static DrugController field called drugController.
  • The Solution class must have a public static boolean field called isStopped.
  • The Solution class must have a private static void waitAMoment() method that waits 100 ms.
  • The Pharmacy class must implement the Runnable interface.
  • The Pharmacy thread should run as long as isStopped is false.
  • The Pharmacy thread must use drugController to sell a random drug (getRandomDrug) in a random amount (getRandomCount).
  • The Pharmacy thread should use the waitAMoment() method to wait 300 ms between sales.
  • The Person class must implement the Runnable interface.
  • The Person thread should run as long as isStopped is false.
  • The Person thread must use drugController to purchase a random drug (getRandomDrug) in a random amount (getRandomCount).
  • The Person thread should use the waitAMoment() method to wait 100 ms between purchases.
  • The methods of the DrugController class must be synchronized.
package com.codegym.task.task17.task1715; import java.util.ArrayList; import java.util.List; /* Pharmacy Requirements: 6. The Pharmacy thread must use drugController to purchase a random drug (getRandomDrug) in a random amount (getRandomCount). 7. The Pharmacy thread should use the waitAMoment() method to wait 300 ms between purchases. 8. The Person class must implement the Runnable interface. 9. The Person thread should run as long as isStopped is false. 10. The Pharmacy thread must use drugController to sell a random drug (getRandomDrug) in a random amount (getRandomCount). 11. The Person thread should use the waitAMoment() method to wait 100 ms between purchases. 12. The methods of the DrugController class must be synchronized. */ public class Solution { public static DrugController drugController = new DrugController(); public static boolean isStopped = false; public static void main(String[] args) throws InterruptedException { Thread pharmacy = new Thread(new Pharmacy()); Thread man = new Thread(new Person(), "Man"); Thread woman = new Thread(new Person(), "Woman"); pharmacy.start(); man.start(); woman.start(); Thread.sleep(1000); isStopped = true; } public static class Pharmacy implements Runnable{ public void run(){ while(!isStopped){ drugController.buy(getRandomDrug(),getRandomCount()); waitAMoment(); } } } public static class Person implements Runnable{ public void run(){ while(!isStopped){ drugController.sell(getRandomDrug(),getRandomCount()); waitAMoment(); } } } public static int getRandomCount() { return (int) (Math.random() * 3) + 1; } public static Drug getRandomDrug() { int index = (int) ((Math.random() * 1000) % (drugController.allDrugs.size())); List<Drug> drugs = new ArrayList<>(drugController.allDrugs.keySet()); return drugs.get(index); } private static void waitAMoment() { for(int i = 0; i < 3; i++) waitAMoment(); } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Ivaylo Trifonov
Level 22 , Madrid, Spain
9 August 2019, 15:25
Well, the first requirement that You don't meet says that You have to wait (use sleep()) for 300ms and in the same tame use the waitAMoment() method who waits only 100ms. You don't have to change the method otherwise will cause an error. In this case, consider the use of some loop only when it is needed (but not inside the waitAMinute method code block). Changing the code will clear the other two errors in Your revision because Your change makes the two run() methods to run for longer then required. Leave the waitAMinute() like it was and implement the loop where is it required more wait (sleep()) time.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.