本文介绍了Java多线程:如何使线程等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一架飞机(飞机)从一个机场飞到另一个飞机场。当飞机起飞时,机场被阻挡5秒。 (如果另一架飞机要着陆或起飞 - 必须等待)。如果一架飞机到达目的地,它就会降落(如果机场没有被另一架飞机挡住)然后乘坐随机机场飞到那里等等......
我在评论中有疑问 - 如何让线程等待?我的代码还有什么问题?
这是我的班级Samolot aka Plane:

I want to make a plane (Thread) fly from one airfield to another. When a plane is starting the airfield is blocked for 5 sec. (if another plane wants to land or take off - must wait). If a plane reach its destination it lands (if airfield is not blocked by another plane) and then takes a random airfield and fly there and so on....I have questions in comments - how to make a thread wait ? And what else is wrong with my code ?Here is my class Samolot aka Plane:

     public class Samolot extends Thread{

        int id;
        double paliwo;
        Lotnisko source; //Lotnisko aka airfield
        Lotnisko dest;
      double xPosition;
      double yPosition;
        double xTarget;
      double yTarget;

    public Samolot(Lotnisko source, Lotnisko dest) {


        this.source = source;
        this.dest = dest;
        paliwo = 100;

    }
public void run(){
         while(true){

            tryStart();


         }
     }


     public void tryStart(){
         if(source.pas == true){  // if true the airfield is not blocked and we can go
            source.timer();      // its a method in class Lotnisko, makes pas = false for 8sec
            lecimy(source, dest);
         }
         else if(source.pas == false){
             // how to make a thread wait ?
         }
     }

public void tryLadowanie(){
         if(dest.pas == true){
             dest.timer();

            source = dest;
            dest = Rand(source);
            tryStart();
        }
         else if(dest.pas == false){
             //how to make a thread wait ?
         }



     }
     public void lecimy(Lotnisko source, Lotnisko dest){

        xPosition = source.coords.getX();
        yPosition = source.coords.getY();
        xTarget = dest.coords.getX();
        yTarget = dest.coords.getY();


        while( (xPosition != xTarget) && (yPosition != yTarget) ){
            update();

            try{

                sleep(100);// ok
                }
            catch (InterruptedException e) {
                System.out.println("Error");
                                    }
     }
     tryLadowanie();
     }



    public void update() {

        paliwo -= 0.05;
        double dx = xTarget - xPosition;
        double dy = yTarget - yPosition;
        double length = sqrt(dx*dx+dy*dy);

        dx /= length;
        dy /= length;

        if (Math.abs(dest.coords.getX() - source.coords.getX()) < 1)
            dx = 0;
        if (Math.abs(dest.coords.getY() - source.coords.getY()) < 1)
            dy = 0;
            xPosition += dx;
            yPosition += dy;
  }

    public Point getPositions() {
        Point curPos = new Point((int)xPosition, (int)yPosition);

        return curPos;
    }


推荐答案

好的,所以你的飞机是一个线程和你的机场是共享资源。因此,要使您的飞机(线程)等待,您需要在共享资源(机场)上进行同步。你可能会做这样的事情。

OK, so your plane is a thread and your airfields are the shared resource. So to make your plane (thread) wait, you'll need to synchronized on the shared resource (airfield). You'll probably do something like this.

起飞时,

public void tryStart() {
    synchronized(source) { // try to obtain source lock
        try {
            Thread.sleep(5000); // lock it for 5 seconds.
        }
        catch(Exception ignore) {
            // You'll need to decide what to do if something interrupts the thread while it's "sleeping" (ie. locked) on the source airfield. Normally, this should not happen.
        }
    }
    // After 5 seconds, releases lock on airfield "source" and Plane starts flying
}

登陆时,

public void tryLadowanie() {
    synchronized(dest) { // try to obtain dest lock
        // successfully obtained lock.
        // perform your landing
    }
    // plane landed and releases "dest" resource for other planes (ie. threads)
}

更全面的飞机飞行图片。

For a more complete picture of plane flying.

public void run(){
    while(true){
        tryStart(); // take off from source
        lecimy();   // fly the plane. Method will only return when plane reaches destination.
        tryLadowanie(); // land the plane
        source = dest;
        dest = Rand(source); // returns a new destination but can't be the same as source
    }
}


public void tryStart(){
    synchronized(source) {
        try {
            Thread.sleep(5000);
        }
        catch(Exception ignore) { }
    }
}

public void tryLadowanie(){
    synchronized(dest) {
        // land the plane
    }
}

这篇关于Java多线程:如何使线程等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:17