开发者

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. But the thing is that now I want to have contr开发者_JAVA技巧ol of all the threads so that I can run each thread alone by a certain quantum (or time), just like real OS processes schedulers.

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?


Yes, there is:

Object.wait( ), Object.notify() and a bunch of other much nicer synchronization primitives in java.util.concurrent.


Who said Java is not low level enough?

Here is my 3 minute solution. 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.

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.Thread to represent the running threads so it may as well all be done in a lower-level language like C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜