Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public class Solution {
  2. public static int totalSpeechCount = 200;
  3. public static int utterancesPerSpeech = 1000000;
  4.  
  5. public static void main(String[] args) throws InterruptedException {
  6. Politician ivanov = new Politician("Иванов");
  7. ivanov.join();
  8. Politician petrov = new Politician("Петров");
  9. Politician sidorov = new Politician("Сидоров");
  10.  
  11. while (ivanov.getSpeechCount() + petrov.getSpeechCount() + sidorov.getSpeechCount() < totalSpeechCount) {
  12. }
  13.  
  14. System.out.println(ivanov);
  15. System.out.println(petrov);
  16. System.out.println(sidorov);
  17. }
  18.  
  19. public static class Politician extends Thread {
  20. private volatile int utteranceCount;
  21.  
  22. public Politician(String name) {
  23. super(name);
  24. start();
  25. }
  26.  
  27. public void run() {
  28. while (utteranceCount < totalSpeechCount * utterancesPerSpeech) {
  29. utteranceCount++;
  30. }
  31. }
  32.  
  33. public int getSpeechCount() {
  34. return utteranceCount / utterancesPerSpeech;
  35. }
  36.  
  37. @Override
  38. public String toString() {
  39. return String.format("%s сказал речь %d раз", getName(), getSpeechCount());
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement