英文:
Execution priority of threads in waiting queue and entry queue when calling notifyAll
问题
以下是你要翻译的内容:
我对在调用特定对象上的notifyAll后等待线程执行顺序有点难以理解。稍后添加了一个代码示例,我将引用该示例。
简而言之,我能否确信,如果等待队列中有100个名为“ABC”的线程,入口队列中有100个名为“GHI”的线程,所有“ABC”线程都将在“GHI”线程之前执行?
当我运行这个简短的程序时,似乎总是这种情况,因为“GHI”线程总是打印出“0”,这表示所有“ABC”线程在那时都已执行完毕。我只是想知道,这是我的系统/实现特定的,还是我可以确信这总是会发生?在Java文档中,我没有找到关于这一点的保证,但也许我没有在正确的地方搜索。
这是我所参考的简短示例代码,对我来说始终会产生一致的结果:
public class Main {
private volatile int count = 0;
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
// 添加100个将都进入等待状态的线程
main.addThreads("ABC");
// 等待100个线程被创建,然后在休眠10秒后通知所有线程
main.waitForThreads();
// 确保线程已经成功启动,且持有锁并在休眠
Thread.sleep(1000);
// 向入口队列中添加更多的消费者线程
main.addThreads("GHI");
// 等待线程完成
Thread.sleep(15000);
}
private void waitForThreads() throws InterruptedException {
while (this.count < 99) {
System.out.println("尚未有足够的等待线程");
Thread.sleep(1000);
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Main.this.syncMethod();
} catch (InterruptedException e) {
}
}
}, "DEF");
t.start();
}
private void addThreads(String threadName) {
for(int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Main.this.syncMethod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}}, threadName).start();;
}
}
private synchronized void syncMethod() throws InterruptedException {
String name = Thread.currentThread().getName();
if (name.equals("ABC")) {
this.count++;
this.wait();
this.count--;
} else if (name.equals("DEF")) {
Thread.sleep(10000);
this.notifyAll();
System.out.println("已通知等待线程");
} else {
System.out.println(this.count);
}
}
}
英文:
I'm struggling a bit to understand the order in which waiting threads get executed once I call notifyAll on a certain object. There is a code example added later, which I will be referencing.
In short, can I be sure that if I have 100 threads named "ABC" in the waiting queue and 100 threads named "GHI" in the entry queue, all "ABC" threads will be executed before "GHI" threads?
When I'm running the short program, it seems that's always the case since "GHI" threads always print out "0", which denotes all "ABC" threads have finished execution by then. I'm just wondering - is this my system/implementation specific or can I be sure this will always be the case? From Java documentation I could not find guarantees about this - but maybe I didn't search in the right place.
Here is the short sample code I was referencing and which always gives consistent results for me:
public class Main {
private volatile int count = 0;
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
// add 100 threads that will all go to wait
main.addThreads("ABC");
// wait for 100 threads to be created and then notify all after sleeping 10s
main.waitForThreads();
// make sure thread has managed to start and is holding the lock and slpeeping
Thread.sleep(1000);
// add a lot more consumers to entry queue
main.addThreads("GHI");
//wait for threads to finish
Thread.sleep(15000);
}
private void waitForThreads() throws InterruptedException {
while (this.count < 99) {
System.out.println("Not enough waiting threads yet");
Thread.sleep(1000);
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Main.this.syncMethod();
} catch (InterruptedException e) {
}
}
}, "DEF");
t.start();
}
private void addThreads(String threadName) {
for(int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Main.this.syncMethod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}}, threadName).start();;
}
}
private synchronized void syncMethod() throws InterruptedException {
String name = Thread.currentThread().getName();
if (name.equals("ABC")) {
this.count++;
this.wait();
this.count--;
} else if (name.equals("DEF")) {
Thread.sleep(10000);
this.notifyAll();
System.out.println("Waiting threads notified");
} else {
System.out.println(this.count);
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论