gpt4 book ai didi

java - 为什么这个线程示例不起作用?都是 wait() 的

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:36 25 4
gpt4 key购买 nike

下面的代码是为了模拟我正在使用的机器人模拟器。我不完全确定为什么这不起作用 - 我对线程不是很熟悉,尽管我今天已经尝试阅读大量内容,但我似乎没有取得进展。问题是一旦调用了 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;
}
}

最佳答案

您对“pauseDistanceSensor”的调用会在等待调用时阻塞主线程。

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

您的 pause 方法应该改为调用同步方法,使用 boolean 标志或类似的方法暂停传感器运行循环。

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

关于java - 为什么这个线程示例不起作用?都是 wait() 的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3098119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com