gpt4 book ai didi

Java 线程 : Given program isn't behaving properly

转载 作者:搜寻专家 更新时间:2023-11-01 03:17:45 25 4
gpt4 key购买 nike

import java.lang.*;

class MyRun implements Runnable
{
public static int start = 0;
public void run()
{
int myCounter = 0;
System.out.println(Thread.currentThread().getName()+" is waiting on start flag");
while(start<1)
{
// just waiting...
}
while(myCounter<5)
{
myCounter++;
System.out.println(Thread.currentThread().getName()+" : "+myCounter);
try
{
Thread.sleep(1000);
} catch(Exception ex) {}
}
System.out.println(Thread.currentThread().getName()+" stopped and start="+start);
}
}

public class test
{
public static void main(String[] args)
{
Thread[] threads = new Thread[10];
for(int i=0; i<10; i++)
{
int p = i+1;
threads[i] = new Thread(new MyRun());
threads[i].setName("TH"+p);
threads[i].setPriority(p);
threads[i].start();
}
MyRun.start=1; // signaling GO
}
}

输出:

TH1 is waiting on start flag
TH2 is waiting on start flag
TH3 is waiting on start flag
TH4 is waiting on start flag
TH5 is waiting on start flag
TH6 is waiting on start flag
TH8 is waiting on start flag
TH7 is waiting on start flag
TH9 is waiting on start flag
TH10 is waiting on start flag
TH10 : 1
TH10 : 2
TH10 : 3
TH10 : 4
TH10 : 5
TH10 stopped and start=1

我期待所有线程的输出,因为开始标志设置为 1,但只执行名称为 TH10 的线程。

你能帮我找出这段代码中到底发生了什么吗?

我想用这样的代码做什么?

答案:尝试分析线程的优先级(清除我的线程概念,特别是多线程与可运行的共享静态变量)。

提前致谢,如果您需要我这边的任何信息,请告诉我。

更新:将所有线程视为马匹,当 MyRun.start 设置为 1(应将其视为所有马匹的 GO 信号)但只有第 10 匹马在信号后运行。

最佳答案

这段代码至少有两个问题:

  • 它使用公共(public)非同步变量 (start),就多线程而言,它始终是一种代码味道 => 无法保证您的线程看到 的正确值开始
  • 它不等待线程完成它们的任务

实现这种事情的正确方法是使用 java.util.concurrent 包中的范例。在这种情况下,这将是 CountDownLatches

Javadoc 中提供的示例实际上非常接近您在这里要实现的目标(一起启动所有线程,等待它们完成),因此我正在复制它。 MyRun就是下面的Worker;在doWork()中,请实现你的run()方法的内容

startSignal“锁定”所有“等待”它的线程的开始。doneSignal 在所有线程结束“倒计时”后“锁定”程序(main)的结束

 class Driver { // ...
void main() throws InterruptedException {
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);

for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();

doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
}
}

class Worker implements Runnable {
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;
Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}
public void run() {
try {
startSignal.await();
doWork();
doneSignal.countDown();
} catch (InterruptedException ex) {} // return;
}

void doWork() { ... }
}

关于Java 线程 : Given program isn't behaving properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524519/

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