如何无限期地暂停 Java 中的线程然后再恢复它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2622804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to indefinitely pause a thread in Java and later resume it?
提问by meteorfox
Maybe this question has been asked many times before, but I never found a satisfying answer.
也许这个问题之前已经被问过很多次了,但我一直没有找到令人满意的答案。
The problem:
问题:
I have to simulate a process scheduler, using the round robin strategy. I'm using threads to simulate processes and multiprogramming; everything works fine with the JVM managing the threads. But the thing is that now I want to have control of all the threads so that I can run each thread alone by a certain quantum (or time), just like real OS processes schedulers.
我必须使用循环策略来模拟进程调度程序。我正在使用线程来模拟进程和多道程序;JVM 管理线程一切正常。但问题是,现在我想要控制所有线程,以便我可以按特定量程(或时间)单独运行每个线程,就像真正的 OS 进程调度程序一样。
What I'm thinking to do:
我想做什么:
I want have a list of all threads, as I iterate the list I want to execute each thread for their corresponding quantum, but as soon the time's up I want to pause that thread indefinitely until all threads in the list are executed and then when I reach the same thread again resume it and so on.
我想要一个所有线程的列表,当我迭代列表时,我想为其相应的量子执行每个线程,但是一旦时间到了,我想无限期地暂停该线程,直到执行列表中的所有线程,然后当我再次到达同一个线程恢复它等等。
The question:
问题:
So is their a way, without using deprecated methods stop(), suspend(), or resume(), to have this control over threads?
那么他们有没有一种不使用不推荐使用的方法 stop()、suspend() 或 resume() 来控制线程的方法呢?
采纳答案by Roman
Who said Java is not low level enough?
谁说Java不够low?
Here is my 3 minute solution. I hope it fits your needs.
这是我的 3 分钟解决方案。我希望它符合您的需求。
import java.util.ArrayList;
import java.util.List;
public class ThreadScheduler {
private List<RoundRobinProcess> threadList
= new ArrayList<RoundRobinProcess>();
public ThreadScheduler(){
for (int i = 0 ; i < 100 ; i++){
threadList.add(new RoundRobinProcess());
new Thread(threadList.get(i)).start();
}
}
private class RoundRobinProcess implements Runnable{
private final Object lock = new Object();
private volatile boolean suspend = false , stopped = false;
@Override
public void run() {
while(!stopped){
while (!suspend){
// do work
}
synchronized (lock){
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
}
public void suspend(){
suspend = true;
}
public void stop(){
suspend = true;stopped = true;
synchronized (lock){
lock.notifyAll();
}
}
public void resume(){
suspend = false;
synchronized (lock){
lock.notifyAll();
}
}
}
}
Please note that "do work" should not be blocking.
请注意,“做工作”不应阻塞。
回答by Alexander Pogrebnyak
Yes, there is:
就在这里:
Object.wait( ), Object.notify() and a bunch of other much nicer synchronization primitives in java.util.concurrent.
Object.wait()、Object.notify() 和一堆其他更好的同步原语java.util.concurrent。
回答by Andrzej Doyle
Short answer: no. You don't get to implement a thread scheduler in Java, as it doesn't operate at a low enough level.
简短的回答:没有。您无法在 Java 中实现线程调度程序,因为它的运行级别不够低。
If you really do intend to implement a process scheduler, I would expect you to need to hook into the underlying operating system calls, and as such I doubt this will ever be a good idea (if remotely possible) in Java. At the very least, you wouldn't be able to use java.lang.Threadto represent the running threads so it may as well all be done in a lower-level language like C.
如果您真的打算实现进程调度程序,我希望您需要挂钩底层操作系统调用,因此我怀疑这在 Java 中永远是一个好主意(如果远程可能的话)。至少,您将无法使用java.lang.Thread来表示正在运行的线程,因此它也可以全部使用像 C 这样的低级语言来完成。

