简体   繁体   English

如何在Java中无限期地暂停一个线程然后恢复它?

[英]How to indefinitely pause a thread in Java and later resume it?

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. 一切正常,JVM管理线程。 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. 但问题是,现在我想控制所有线程,这样我就可以通过一定量(或时间)单独运行每个线程,就像真正的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()来控制线程吗?

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的一堆其他更好的同步原语。

Who said Java is not low level enough? 谁说Java不够低?

Here is my 3 minute solution. 这是我的3分钟解决方案。 I hope it fits your needs. 我希望它符合您的需求。

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. 请注意,“工作”不应该阻止。

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. 如果你真的打算实现一个进程调度程序,我希望你需要挂钩底层的操作系统调用,因此我怀疑这在Java中是一个好主意(如果可能的话)。 At the very least, you wouldn't be able to use java.lang.Thread to represent the running threads so it may as well all be done in a lower-level language like C. 至少,你将无法使用java.lang.Thread来表示正在运行的线程,所以它也可以用像C这样的低级语言来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM