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

时间:2014-01-24 11:02:39

标签: java multithreading

我想让一架飞机(飞机)从一个机场飞到另一个飞机场。当飞机起飞时,机场被阻挡5秒。 (如果另一架飞机想降落或起飞 - 必须等待)。如果一架飞机到达目的地,它将降落(如果机场没有被另一架飞机阻挡),然后乘坐随机机场飞到那里等等...... 我在评论中有疑问 - 如何让线程等待?我的代码还有什么问题? 这是我的班级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;
    }

3 个答案:

答案 0 :(得分:2)

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

起飞,

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

答案 1 :(得分:0)

不是一个完整的答案,但等待方法用于等待线程,通知用于唤醒正在等待的线程。

检查以获取有关等待和通知方法的更多信息: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait() http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notify()

答案 2 :(得分:0)

使用通用java对象exp。 Object lock = new Object(); 等待 : 同步(锁) {   lock.wait(); } 从另一个线程释放或释放锁定 在同一个锁定对象上 同步(锁) {   lock.notify(); }