gpt4 book ai didi

java - 线程对象不同步

转载 作者:行者123 更新时间:2023-12-01 12:14:25 25 4
gpt4 key购买 nike

所以我编写了这个程序来运行这个机器人类的多个线程,使用 cyclobarrier 来同步线程。由于我对 cyclobarrier 知之甚少,我认为它会自动同步我的线程,但似乎不会。我需要做什么才能让我的机器人线程根据我的进度整数值进行同步?

public class Robot implements Runnable{
public static final int on = 0x0001;
public static final int clockwise = 0x0002;
public static final int counter = 0x0004;
int opcode;
int moveCount = 0;
int rotation, increment, progress = 0;
boolean CW;

ProgressBar prgBar;
CyclicBarrier cyclicBarrier;
Controller c;
Motor m;

public Robot(CyclicBarrier cyclicBarrier, Motor m)
{
opcode = 0;
this.cyclicBarrier = cyclicBarrier;
this.m = m;
}




public void run(){
System.out.println("Running: ");

try {
while(progress <= 24){
i = m.Engage(this, i, increment/4);
prgBar.setProgress(this, progress);
}
System.out.println("Sleeping: ");
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+ " waiting at barrier 1");
cyclicBarrier.await();

while(progress <= 49){
i = m.Engage(this, i, increment/2);
prgBar.setProgress(this, progress);
}
System.out.println("Sleeping: ");
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+ " waiting at barrier 2");
cyclicBarrier.await();

while(progress <= 74){
i = m.Engage(this, i, ((increment/4)*3));
prgBar.setProgress(this, progress);
}
System.out.println("Sleeping: ");
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+ " waiting at barrier 3");
cyclicBarrier.await();

while(progress <= 99){
i = m.Engage(this, i, increment);
prgBar.setProgress(this, progress);
}
System.out.println("Sleeping: ");
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+ " waiting at barrier 4");
cyclicBarrier.await();

} catch (Exception e){
e.printStackTrace();
}
prgBar.setProgress(this, progress);
System.out.println("Engaging: ");
}

公共(public)类 Controller {

public static void main(String[] args){
CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
Motor m = new Motor();


Robot xRob = new Robot(cyclicBarrier, m);
Robot yRob = new Robot(cyclicBarrier, m);
Robot zRob = new Robot(cyclicBarrier, m);

Thread xRobThread = new Thread(xRob);
Thread yRobThread = new Thread(yRob);
Thread zRobThread = new Thread(zRob);

boolean clockwise = true, counterClockwise = false;




m.setMotor(clockwise, 14400, xRob);
m.setMotor(clockwise, 7200, yRob);
m.setMotor(counterClockwise, 28800, zRob);

xRobThread.start();
yRobThread.start();
zRobThread.start();

try {
xRobThread.join();
yRobThread.join();
zRobThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}


System.out.printf("x = %d y = %d z = %d\n\n", xRob.moveCount, yRob.moveCount, zRob.moveCount);

}
}

最佳答案

程序运行时会做什么?您期望它做什么? (即,您认为“同步我的线程”是什么意思?)

至少,您的更新现在包含足够的信息供我猜测,并且我猜测程序挂起。您的 main() 例程为四方创建一个新的 CyclicBarrier,然后创建三个线程。每个线程都调用 cycloBarrier.await(),但我没有看到任何第四方。在四个线程调用await() 之前,屏障不会打开。

<小时/>

其他信息:

好吧,假设您有一个循环可以取得某种“进展”。您可以通过执行以下操作,让第一个达到特定进度阈值的线程等待其他线程 catch :

double progress = 0.0;
static final double CHECKPOINT = 0.5;
static final double FINISHED = 1.0;
boolean reachedCheckpoint = false;

while (progress < FINISHED) {
progress = makeSomeProgress();
if (! reachedCheckpoint && progress >= CHECKPOINT) {
cyclicBarrier.await();
reachedCheckpoint = true;
}
}

显然,如果你想要有多个检查点,程序会稍微复杂一些,但这就是基本思想。请记住,为其构造 CyclicBarrier 的“各方”数量必须完全等于实际使用它的线程数量。

关于java - 线程对象不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099795/

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