Java多线程:如何使线程等待?

用户名

我想让一架飞机从一个机场飞到另一个机场。飞机起飞时,飞机场被封锁5秒钟。(如果另一架飞机要降落或起飞-必须等待)。如果一架飞机到达目的地,它会降落(如果飞机场没有被另一架飞机挡住),然后进入一个随机的飞机场然后飞到那里,依此类推....我在评论中有疑问-如何使线程等待?我的代码还有什么问题呢?这是我的课萨莫洛特又名飞机:

     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;
    }
匿名

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

为了起飞

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)
}

有关飞机飞行的更完整图片。

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
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章