Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. package javaapplication5;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. Random rnd = new Random();
  8. int count = 0;
  9. float[] speed = new float[]{300,500,800};
  10. Airplane[] airplane = new Airplane[20];
  11. Operator operator = new Operator(airplane);
  12.  
  13.  
  14. for (count = 0; count<20; count++)
  15. {
  16. airplane[count] = new Airplane(speed[rnd.nextInt(speed.length)] / 60, operator, count);
  17. }
  18. }
  19. }
  20. class Airplane extends Thread{
  21. float speed;
  22. float distance;
  23. int number;
  24. boolean turn;
  25. int route;
  26. int index;
  27. Operator operator;
  28.  
  29. public Airplane(float speed_, Operator operator_, int index_){
  30. speed = speed_;
  31. distance = 0;
  32. number = 0;
  33. turn = false;
  34. index = index_;
  35. operator = operator_;
  36. }
  37.  
  38. public synchronized void Check(){
  39. if (Operator.BUZY) {
  40. try {
  41. wait();
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. else
  47. {
  48. if (number == 0){
  49. if(operator.Call(this)) {
  50. Transfer();
  51. }
  52. else
  53. {
  54. try {
  55. Thread.sleep(1000);
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. else {
  62. if (distance >= number*10 && !turn){
  63. if (operator.Call(this)){
  64. Transfer();
  65. }
  66. else {
  67. Turn();
  68. }
  69. }
  70. else if (distance >= (number-1)*10 && turn){
  71. Turn();
  72. }
  73. }
  74. }
  75. }
  76.  
  77. public void Move(){
  78. try {
  79. Thread.sleep(1000);
  80. } catch(InterruptedException ex) {
  81. Thread.currentThread().interrupt();
  82. System.out.println("fuck " + index);
  83. }
  84. if (turn){
  85. distance-=speed;
  86. }
  87. else{
  88. distance+=speed;
  89. }
  90. }
  91.  
  92. public void Transfer(){
  93. number++;
  94. System.out.println("Самолет "+ index +" перешел на следующий " + number + " участок, маршрут " + route);
  95. }
  96.  
  97. public void Turn(){
  98. System.out.println("Самолет "+ index + " кружит.");
  99. turn = !turn;
  100. }
  101.  
  102. public void run(){
  103. while (true) {
  104. if (number<150) {
  105. if (number!=0) Move();
  106. Check();
  107. }
  108. else {
  109. System.out.println("Самолет " + index + " закончил полет.");
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. class Operator extends Thread{
  116. int [][] path = new int[150][3];
  117. Airplane[] airplane;
  118. public static boolean BUZY = false;
  119.  
  120. Operator (Airplane[] airplane_) {
  121. airplane = airplane_;
  122. }
  123.  
  124. public synchronized boolean Call(Airplane airplane) {
  125. BUZY = true;
  126. System.out.println("Самолет "+ airplane.index +" связался с диспетчером.");
  127. try {
  128. Thread.sleep(1000);
  129. } catch(InterruptedException ex) {
  130. System.out.println("fuck " + airplane.index);
  131. Thread.currentThread().interrupt();
  132. }
  133.  
  134. if (airplane.number==0) {
  135. System.out.println("Самолет " + airplane.index + " запросил взлет.");
  136. for (int i = 0; i<3; i++){
  137. if (path[0][i]==0) {
  138. airplane.route = i+1;
  139. path[0][i] = 1;
  140. BUZY = false;
  141. notifyAll();
  142. return true;
  143. }
  144. else
  145. {
  146. try {
  147. airplane.wait();
  148. BUZY = false;
  149. return false;
  150. } catch (InterruptedException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. }
  155. }
  156. else {
  157. for (int i = 0; i<3; i++){
  158. if (path[airplane.number][i]==0) {
  159. path[airplane.number - 1][airplane.route - 1] = 0;
  160. airplane.route = i+1;
  161. if (airplane.number < 148) {
  162. path[airplane.number][i] = 1;
  163. }
  164. else {
  165. path[airplane.number][i] = 0;}
  166. BUZY = false;
  167. notifyAll();
  168. return true;
  169. }
  170. }
  171. }
  172. BUZY = false;
  173. notifyAll();
  174. return false;
  175. }
  176.  
  177. public void run()
  178. {
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement