How can we make a thread to sleep for a infinite time in java?

19.8k views Asked by At

Pls help me to understand how can we make a thread to sleep for a infinite time period .

7

There are 7 answers

4
Liviu Stirb On BEST ANSWER

I can't think of a good reason for doing this. As one of the comments noted Long.MAX_VALUE is roughly 292 billion years so probably Thread.sleep(Long.MAX_VALUE) is enough. But if you want a theoretical infinite sleep solution:

while (true) {
    Thread.sleep(Long.MAX_VALUE);
}
3
JFPicard On

Make it wait for a mutex or resource that will never be released. It's the deadlock principle. The better way is to make the thread to finish his code, so it will end and not be started again.

Edit: I don't recommand an infinite loop since it's the pooling principle. It will consume a lot of resources for nothing.

0
Stephen C On

Literally, you can't. No Java application can run for an infinite amount of time. The hardware will die first :-)

But in practice1, the following will sleep until the JVM terminates ... or the thread is interrupted.

 public void freeze() throws InterruptedException {
    Object obj = new Object();
    synchronized (obj) {
        obj.wait();
    }
 }

If you wanted to you could catch the exception within a while (true) loop. And doing the same with "sleep(max int)" is equivalent.

But frankly, making a thread go to sleep "for ever" is wasteful2, and probably a bad idea. I have no doubt that there will be better ways to achieve what you are really trying to do.


1 - These solutions I talk about are for when a thread needs to make itself go to sleep. If you one thread to unilaterally make a different thread go to sleep, it can't do that safely. You could use the deprecated Thread.suspend() method, but it is dangerous, and it may not be available on future Java platforms.

2 - A thread stack occupies a significant amount of memory, and it cannot be released until the thread terminates.

0
Harrison K.M On

It's actually quite easy if you do it this way:

public static boolean timerController = false;
Timer timer = new Timer();
public TimerTask task = new TimerTask() {
    
    public void run() {
        if(timerController == false){
        tracker();
        t.setText("<html><br/>Day " + day + ", hour " + hour + "<br/>");
        System.out.println("Hour: " + hour + " Day: " + day + " Real time seconds " + realTime + " Seconds");}
    }
    
};

public void start() {

    timer.scheduleAtFixedRate(task, 1000, 1000);

}   

public void pause(){
timerController = true;
}

public void resume(){
timerController = false;
}

Make a timer object in another class, and simply start, pause, and resume with the three methods. It should "stop" when you pause the timer, and you won't need to deal with any exception handling or any try/catch statements!

1
Taylor Gautier On
Thread.currentThread().join();

Will sleep until the JVM is killed.

0
Sathesh On

Just make an infinite loop

while(true){}
0
k13i On

You can use class CyclicBarrier from the JDK.

new CyclicBarrier(2).await();

Constructor argument is the number of threads that must invoke await method before the barrier is reached.