Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import mk.ukim.finki.os.synchronization.ProblemExecution;
  2. import mk.ukim.finki.os.synchronization.TemplateThread;
  3.  
  4. import java.util.Date;
  5. import java.util.HashSet;
  6. import java.util.Scanner;
  7.  
  8. public class MusicBand {
  9.  
  10.  
  11. static MusicBandState state = new MusicBandState();
  12.  
  13. public static class GuitarPlayer extends TemplateThread {
  14.  
  15. public GuitarPlayer(int numRuns) {
  16. super(numRuns);
  17. }
  18.  
  19. @Override
  20. public void execute() throws InterruptedException {
  21. state.play();
  22. state.evaluate();
  23. }
  24.  
  25. }
  26.  
  27. public static class Singer extends TemplateThread {
  28.  
  29. public Singer(int numRuns) {
  30. super(numRuns);
  31. }
  32.  
  33. @Override
  34. public void execute() throws InterruptedException {
  35. state.play();
  36.  
  37. }
  38.  
  39. }
  40.  
  41. public static void init() {
  42.  
  43. }
  44.  
  45. public static void main(String[] args) {
  46. for (int i = 0; i < 10; i++) {
  47. run();
  48. }
  49. }
  50.  
  51. public static void run() {
  52. try {
  53. Scanner s = new Scanner(System.in);
  54. int numRuns = 1;
  55. int numIterations = 100;
  56. s.close();
  57.  
  58. HashSet<Thread> threads = new HashSet<Thread>();
  59.  
  60. for (int i = 0; i < numIterations; i++) {
  61. Singer singer = new Singer(numRuns);
  62. threads.add(singer);
  63. GuitarPlayer gp = new GuitarPlayer(numRuns);
  64. threads.add(gp);
  65. gp = new GuitarPlayer(numRuns);
  66. threads.add(gp);
  67. singer = new Singer(numRuns);
  68. threads.add(singer);
  69. gp = new GuitarPlayer(numRuns);
  70. threads.add(gp);
  71. }
  72.  
  73. init();
  74.  
  75. ProblemExecution.start(threads, state);
  76. System.out.println(new Date().getTime());
  77. } catch (Exception ex) {
  78. ex.printStackTrace();
  79. }
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement