Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.concurrent.Semaphore;
  5.  
  6. public class CriminalTransport {
  7.  
  8.  
  9. public static void main(String[] args) throws InterruptedException {
  10. HashSet<Thread> threads = new HashSet<Thread>();
  11. for (int i = 0; i < 60; i++) {
  12. Policeman red = new Policeman();
  13. threads.add(red);
  14. Criminal green = new Criminal();
  15. threads.add(green);
  16. }
  17. // run all threads in background
  18. for(Thread t : threads)
  19. t.start();
  20. // after all of them are started, wait each of them to finish for maximum 1_000 ms
  21. for(Thread t : threads)
  22. t.join(1_000);
  23. // for each thread, terminate it if it is not finished
  24. boolean flag = true;
  25. for(Thread t : threads){
  26. if(t.isAlive()){
  27. t.interrupt();
  28. System.err.println("Terminated transport");
  29. flag = false;
  30. }
  31. }
  32. if(flag)
  33. System.out.println("Finished transport");
  34.  
  35. }
  36.  
  37. public static Semaphore lock = new Semaphore(1);
  38. public static Semaphore allowPolicemen = new Semaphore(2);
  39. public static Semaphore allowCriminals = new Semaphore(2);
  40. public static Semaphore CriminalsHere = new Semaphore(0);
  41. public static int criminals = 0;
  42. public static int policemen = 0;
  43. public static Semaphore allowExit = new Semaphore(0);
  44.  
  45. public static class Policeman extends Thread{
  46.  
  47. public void run(){
  48. try{
  49. execute();
  50. }catch (InterruptedException e){
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. public void execute() throws InterruptedException {
  56. // waits until it is valid to enter the car
  57. allowPolicemen.acquire();
  58. System.out.println("Policeman enters in the car");
  59. CriminalsHere.acquire();
  60. lock.acquire();
  61. policemen++;
  62. if(policemen == 2){
  63. System.out.println("Start driving.");
  64. Thread.sleep(100);
  65. // one policeman prints the this command to notice that everyone can exit
  66. System.out.println("Arrived.");
  67. policemen=0;
  68. allowExit.release(4);
  69. }
  70. lock.release();
  71. // when the four passengers are inside, one policeman prints the starting command
  72. // the exit from the car is allowed after the "Arrived." message is printed
  73. allowExit.acquire();
  74. System.out.println("Policeman exits from the car");
  75. lock.acquire();
  76. policemen++;
  77. lock.release();
  78. CriminalsHere.acquire();
  79. if(policemen==2){
  80. policemen=0;
  81. criminals=0;
  82. allowCriminals.release(2);
  83. allowPolicemen.release(2);
  84. }
  85.  
  86. }
  87.  
  88. }
  89.  
  90. public static class Criminal extends Thread{
  91.  
  92. public void run(){
  93. try{
  94. execute();
  95. }catch (InterruptedException e){
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. public void execute() throws InterruptedException {
  101. // waits until it is valid to enter the car
  102. allowCriminals.acquire();
  103. System.out.println("Criminal enters in the car");
  104. lock.acquire();
  105. criminals++;
  106. if(criminals == 2){
  107. CriminalsHere.release(2);
  108. criminals=0;
  109. }
  110. lock.release();
  111.  
  112. Thread.sleep(100);
  113. // the exit from the car is allowed after the "Arrived." message is printed
  114. allowExit.acquire();
  115. System.out.println("Criminal exits from the car");
  116. lock.acquire();
  117. criminals++;
  118. if(criminals==2){
  119. CriminalsHere.release(2);
  120. criminals=0;
  121. }
  122. lock.release();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement