- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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 匹马在信号后运行。
最佳答案
这段代码至少有两个问题:
start
),就多线程而言,它始终是一种代码味道 => 无法保证您的线程看到 的正确值开始
实现这种事情的正确方法是使用 java.util.concurrent
包中的范例。在这种情况下,这将是 CountDownLatch
es
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/
我想使用结构 DataResponse 作为 JSON() 的参数来响应用户。通过初始化 DataResponse 的实例,我得到了错误消息,给出了太多的参数,但给出了所有必要的参数。 type Da
我正在尝试将 google-one-tap 与本地主机上的 django 项目集成。所以我在 Client ID for Web 的 Authorized JavaScript origins 中添加
考虑一个类A,我如何编写一个具有与相同行为的模板 A& pretty(A& x) { /* make x pretty */ return x; } A pretty(A&& x) {
我正在使用 Hibernate envers 3.6.3.Final。我可以审核表,我可以看到 _audit 表中填充了 revision_number、revision_type 和实体数据。 我正
问题详细描述如下: 给定两个单词(beginWord 和 endWord)和字典的单词列表,找出是否存在从 beginWord 到 endWord 的转换序列,这样: 一次只能更改一个字母 每个转换后
我正在尝试解析任何选定的 mysql 表的单行的所有列字段和数据。 这背后的原因是为任何给定的单行创建一个类似“通用”的表解析器。 例如,我有这个表“tbl1”: +----+------------
我有一个列表,它可能包含也可能不包含重复的元素。给定另一个列表/元素集,我需要该列表中存在的所有唯一元素的列表。 Input: input_list = ['android', 'ios', 'and
需要编写一个算法来查找给定字符串在给定索引处的 Anagram,并按字典顺序排序。例如: Consider a String: ABC then all anagrams are in sorted
给定学生和铅笔的数量,假设学生有 154 名,铅笔有 93 名,如何用 Python 编写代码来获得比率。 输出:x:y 或者说给定两个数字的百分比并找出比率。 输出:x:y 最佳答案 import
给定学生和铅笔的数量,假设学生有 154 名,铅笔有 93 名,如何用 Python 编写代码来获得比率。 输出:x:y 或者说给定两个数字的百分比并找出比率。 输出:x:y 最佳答案 import
作为一名端到端自动化测试人员,我一直认为 Given、When、Then 语句(在使用 Cucumber 时合并到 Gherkin 语言中)应该只按 1.Given、2.When、3 的顺序出现.然后
我正在尝试以动态方式传递参数。我想使用 Perl 函数 given(){},但由于某种原因,我不能在其他任何东西中使用它。这就是我所拥有的。 print(given ($parity) { wh
我想在 cucumber 中测试以下功能。但是,我只想处理输入文件一次(以下功能中的@Given)。但是,它似乎每次都执行@Given 步骤。是否可以在以下功能中仅执行一次此@Given? @file
我想知道是否可以使用 given 参数来自 pytest 的 parametrize 函数。 示例: import pytest from hypothesis import given from h
在deep learning tutorials ,所有训练数据都存储在一个shared数组中,只有该数组的索引被传递给训练函数以切出一个小批量。我知道这允许将数据保留在 GPU 内存中,而不是将小块
我正在尝试运行以下代码: foreach my $k (keys %rec) { #switch for watchlist figures given ($k) { #line 93
我正在尝试在完全支持的情况下使用 GWT 规范,但是它的示例 official documentation有点简单。 在 SO 中搜索我发现了这个问题: Specs2 - How to define
我使用hypothesis 已经有一段时间了。我想知道如何重用 @given parts。 我有一些大约 20 行,我将整个 @given 部分复制到几个测试用例之上。 一个简单的测试例子 @give
我在运行 rspec 文件时不断收到错误: Failures:
让我们调用一个函数 function doSomethingAndInvokeCallback(callback){ // do something callback(); } 我可以
我是一名优秀的程序员,十分优秀!