Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Random rnd = new Random();
  7. int count = 0;
  8. int[] speed = new int[]{300,500,800};
  9. Airplane[] airplane = new Airplane[20];
  10. Operator operator = new Operator(airplane);
  11. Ether ether = new Ether(airplane, operator);
  12.  
  13.  
  14. for (count = 0; count<20; count++)
  15. {
  16. // airplane[count] = new Airplane(speed[rnd.nextInt(speed.length)] / 60, count, ether);
  17. // airplane[count].start();
  18. new Airplane(speed[rnd.nextInt(speed.length)] , count, ether).start();
  19. }
  20. }
  21. }
  22.  
  23. class Airplane extends Thread {
  24. int index;
  25. int distance;
  26. int route;
  27. int roundTime = 10; // поменять на сколько нужно
  28. int flyTime = 10; // поменять на сколько нужно
  29. Ether ether;
  30.  
  31. public Airplane(int speed_, int index_, Ether ether) {
  32. this.index = index_;
  33. this.ether = ether;
  34. flyTime = (10 * 1000 * 60) / speed_;
  35. roundTime = 2 * flyTime;
  36.  
  37. }
  38.  
  39. public void round() {
  40. System.out.println("Airplane " + index +" is rounding" + " on " + distance + " " + route);
  41. try {
  42. sleep(roundTime);
  43. }
  44. catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49. public void moveTo(int dist, int r) {
  50. System.out.println("Airplane " + index +" Moving to " + dist + " " + r);
  51. distance = dist;
  52. route = r;
  53. }
  54.  
  55. public void move() {
  56. try {
  57. sleep(flyTime);
  58. }
  59. catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. @Override
  65. public void run() {
  66. while (distance != 151) {
  67. if (!ether.callOperator(this)) {
  68. round();
  69. } else {
  70. move();
  71. }
  72. }
  73. }
  74. }
  75.  
  76. class Operator {
  77. Airplane[] airplanes;
  78. boolean [][] path;
  79.  
  80.  
  81. public Operator(Airplane[] airplanes) {
  82. this.airplanes = airplanes;
  83. path = new boolean[151][3];
  84. }
  85.  
  86. private int tryFindFreeRoute(int distance) {
  87. if (!path[distance][0]) return 0;
  88. else if (!path[distance][1]) return 1;
  89. else if (!path[distance][2]) return 2;
  90. else return -1;
  91. }
  92.  
  93. public boolean handle(Airplane airplane) {
  94. if (airplane.distance == 150) {
  95. path[airplane.distance][airplane.route] = false;
  96. airplane.moveTo(151, 0);
  97. return true;
  98. }
  99. int route = tryFindFreeRoute(airplane.distance +1);
  100. if (route == -1) return false;
  101. else {
  102. path[airplane.distance][airplane.route] = false;
  103. path[airplane.distance+1][route] = true;
  104. airplane.moveTo(airplane.distance + 1, route);
  105. return true;
  106. }
  107. }
  108. }
  109.  
  110. class Ether {
  111.  
  112. private Operator operator;
  113. private Airplane[] airplanes;
  114.  
  115. public Ether(Airplane[] airplanes, Operator operator) {
  116. this.operator = operator;
  117. this.airplanes = airplanes;
  118. }
  119.  
  120. public synchronized boolean callOperator(Airplane airplane) {
  121. return operator.handle(airplane);
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement