Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication5;
- import java.util.Random;
- public class Main {
- public static void main(String[] args) {
- Random rnd = new Random();
- int count = 0;
- float[] speed = new float[]{300,500,800};
- Airplane[] airplane = new Airplane[20];
- Operator operator = new Operator(airplane);
- for (count = 0; count<20; count++)
- {
- airplane[count] = new Airplane(speed[rnd.nextInt(speed.length)] / 60, operator, count);
- }
- }
- }
- class Airplane extends Thread{
- float speed;
- float distance;
- int number;
- boolean turn;
- int route;
- int index;
- Operator operator;
- public Airplane(float speed_, Operator operator_, int index_){
- speed = speed_;
- distance = 0;
- number = 0;
- turn = false;
- index = index_;
- operator = operator_;
- }
- public synchronized void Check(){
- if (Operator.BUZY) {
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- else
- {
- if (number == 0){
- if(operator.Call(this)) {
- Transfer();
- }
- else
- {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- if (distance >= number*10 && !turn){
- if (operator.Call(this)){
- Transfer();
- }
- else {
- Turn();
- }
- }
- else if (distance >= (number-1)*10 && turn){
- Turn();
- }
- }
- }
- }
- public void Move(){
- try {
- Thread.sleep(1000);
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- System.out.println("fuck " + index);
- }
- if (turn){
- distance-=speed;
- }
- else{
- distance+=speed;
- }
- }
- public void Transfer(){
- number++;
- System.out.println("Самолет "+ index +" перешел на следующий " + number + " участок, маршрут " + route);
- }
- public void Turn(){
- System.out.println("Самолет "+ index + " кружит.");
- turn = !turn;
- }
- public void run(){
- while (true) {
- if (number<150) {
- if (number!=0) Move();
- Check();
- }
- else {
- System.out.println("Самолет " + index + " закончил полет.");
- break;
- }
- }
- }
- }
- class Operator extends Thread{
- int [][] path = new int[150][3];
- Airplane[] airplane;
- public static boolean BUZY = false;
- Operator (Airplane[] airplane_) {
- airplane = airplane_;
- }
- public synchronized boolean Call(Airplane airplane) {
- BUZY = true;
- System.out.println("Самолет "+ airplane.index +" связался с диспетчером.");
- try {
- Thread.sleep(1000);
- } catch(InterruptedException ex) {
- System.out.println("fuck " + airplane.index);
- Thread.currentThread().interrupt();
- }
- if (airplane.number==0) {
- System.out.println("Самолет " + airplane.index + " запросил взлет.");
- for (int i = 0; i<3; i++){
- if (path[0][i]==0) {
- airplane.route = i+1;
- path[0][i] = 1;
- BUZY = false;
- notifyAll();
- return true;
- }
- else
- {
- try {
- airplane.wait();
- BUZY = false;
- return false;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- else {
- for (int i = 0; i<3; i++){
- if (path[airplane.number][i]==0) {
- path[airplane.number - 1][airplane.route - 1] = 0;
- airplane.route = i+1;
- if (airplane.number < 148) {
- path[airplane.number][i] = 1;
- }
- else {
- path[airplane.number][i] = 0;}
- BUZY = false;
- notifyAll();
- return true;
- }
- }
- }
- BUZY = false;
- notifyAll();
- return false;
- }
- public void run()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement