2010-06-23 28 views
3

下面的代码是模拟我正在使用的机器人模拟器。我不完全确定为什么这不起作用 - 我对线程不是很熟悉,即使我今天尝试了大量的阅读,但我似乎没有取得进展。问题是,一旦调用pauseDistanceSensor(),它永远不会醒来。为什么这个线程示例没有工作?这一切等待()的

import java.util.Random; 

public class TestThreads 
{ 
    private DistanceSensor dist; 
    private Thread distanceThread; 
    public TestThreads() 
    { 
     this.dist = new DistanceSensor(); 
     this.distanceThread = new Thread(this.dist); 
     this.distanceThread.start(); 
    } 

    public int getDistance() 
    { 
     return this.dist.getMeasurement(); 
    } 

    public void pauseDistanceSensor() 
    { 
     synchronized(this.dist) 
     { 
      try { 
       this.dist.wait(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void resumeDistanceSensor() 
    { 
     synchronized(this.dist) 
     { 
      this.dist.notify(); 
     } 
    } 

    public static void main(String[] args) 
    { 
     TestThreads test = new TestThreads(); 
     long timestamp = System.currentTimeMillis(); 
     System.out.println("Starting at "+timestamp); 
     System.out.println("1: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("2: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("waiting distance sensor and making 3 getDistance calls then sleeping main thread for 1 second - all 3 getDistance calls should be printed when the sleep ends"); 
     test.pauseDistanceSensor(); 
     System.out.println("3: "+test.getDistance()); 
     System.out.println("4: "+test.getDistance()); 
     System.out.println("5: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("Alive! Notifying the thread"); 
     test.resumeDistanceSensor(); 
     System.out.println("Done at "+System.currentTimeMillis()); 
    } 
} 

class DistanceSensor implements Runnable 
{ 
    private final Random gen = new Random(54); 
    private int currentVal; 
    public DistanceSensor() 
    { 
     this.currentVal = this.gen.nextInt(1500); 
    } 

    public void run() 
    { 
     this.currentVal = this.gen.nextInt(1500); 
    } 

    public int getMeasurement() 
    { 
     return this.currentVal; 
    } 
} 

回答

4

您对'pauseDistanceSensor'的调用会阻塞等待调用的主线程。

此外,您的传感器运行方法只设置一次传感器值; run方法应该循环,每次都要设置这个值。

您的暂停方法应改为使用布尔标志或类似方法调用挂起传感器运行循环的同步方法。

import java.util.Random; 

public class TestThreads 
{ 
    private DistanceSensor dist; 
    private Thread distanceThread; 
    public TestThreads() 
    { 
     this.dist = new DistanceSensor(); 
     this.distanceThread = new Thread(this.dist); 
     this.distanceThread.start(); 
    } 

    public int getDistance() 
    { 
     return this.dist.getMeasurement(); 
    } 

    public void pauseDistanceSensor() 
    { 
     dist.setPaused(true); 
    } 

    public void resumeDistanceSensor() 
    { 
     dist.setPaused(false); 
    } 

    public void finish() { 
     dist.setDone(); 
    } 

    public static void main(String[] args) 
    { 
     TestThreads test = new TestThreads(); 
     long timestamp = System.currentTimeMillis(); 
     System.out.println("Starting at "+timestamp); 
     System.out.println("1: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("2: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("waiting distance sensor and making 3 getDistance calls then sleeping main thread for 1 second - all 3 getDistance calls should be printed when the sleep ends"); 
     test.pauseDistanceSensor(); 
     System.out.println("3: "+test.getDistance()); 
     System.out.println("4: "+test.getDistance()); 
     System.out.println("5: "+test.getDistance()); 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("Alive! Notifying the thread"); 
     test.resumeDistanceSensor(); 
     System.out.println("Done at "+System.currentTimeMillis()); 
     test.finish(); 
    } 
} 

class DistanceSensor implements Runnable 
{ 
    private final Random gen = new Random(54); 
    private int currentVal; 
    private boolean done = false, paused = false; 
    public DistanceSensor() 
    { 
     this.currentVal = this.gen.nextInt(1500); 
    } 

    public void run() 
    { 
     while(!getDone()) { 
      if(!getPaused()) synchronized(this) {this.currentVal = this.gen.nextInt(1500);} 
      synchronized(this) { 
      try { 
       wait(500); 
      } catch(InterruptedException ex) { 
       ex.printStackTrace(); 
      } 
      } 
     } 
    } 

    public synchronized int getMeasurement() 
    { 
     return this.currentVal; 
    } 

    public synchronized boolean getPaused() {return paused;} 
    public synchronized boolean getDone() {return done;} 
    public synchronized void setPaused(boolean p) {paused = p;} 
    public synchronized void setDone() {done = true;notify();} 
} 
+0

当我昨天晚上读到这个消息时,我以为你已经死了。我今天早上跑了它,这是输出.. 从1277311524656/1:1391/2:475 /等待距离传感器和3个getDistance呼叫,然后休眠主线程1秒 - 所有3 getDistance调用应打印时睡眠结束/ 3:114/4:114/5:114 /活!通知线程/完成1277311527716 我想通过暂停DistanceSensor的线程在“生存!通知线程”后显示打印消息。 – nathas 2010-06-23 16:46:39

+0

您必须将打印件移动到传感器线程中。暂停传感器不会暂停印刷完成时的主线程 - 如果已完成,则在主线程恢复之前不会暂停打印调用。 – sje397 2010-06-23 23:25:54

相关问题