Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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?

like image 447
meteorfox Avatar asked Apr 12 '10 14:04

meteorfox


People also ask

How do you make a thread pause indefinitely?

The proper way to suspend a thread indefinitely is to use a ManualResetEvent . The thread is most likely looping, performing some work. The easiest way to suspend the thread is to have the thread "check" the event each iteration, like so: while (true) { _suspendEvent.

How do you pause a resume and thread?

Methods Used:sleep(time): This is a method used to sleep the thread for some milliseconds time. suspend(): This is a method used to suspend the thread. The thread will remain suspended and won't perform its tasks until it is resumed. resume(): This is a method used to resume the suspended thread.

How do you suspend and use a resume in Java?

The suspend() method of thread class puts the thread from running to waiting state. This method is used if you want to stop the thread execution and start it again when a certain event occurs. This method allows a thread to temporarily cease execution. The suspended thread can be resumed using the resume() method.


2 Answers

Yes, there is:

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

like image 196
Alexander Pogrebnyak Avatar answered Oct 09 '22 07:10

Alexander Pogrebnyak


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.

like image 21
Roman Avatar answered Oct 09 '22 07:10

Roman