Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Thread to Sleep for 30 minutes

Tags:

I want to make my thread to wait for 30 minutes. Are there any problems of doing this?

like image 278
venkata lokesh Avatar asked Jul 22 '14 05:07

venkata lokesh


People also ask

Is it a good practice to use thread sleep?

It is not a good practice to use Thread. Sleep method for synchronizing the application under test with our script. The good practice is, use explicit wait if you targeting a particular element. If you want to target the most of the elements in the page, then use Implicit wait.

How long should a thread sleep?

If it is a UI worker thread, as long as they have some kind of progress indicator, anywhere up to half a second should be good enough. The UI should be responsive during the operation since its a background thread and you definitely have enough CPU time available to check every 500 ms.

How do I make my thread sleep for 1 minute?

To make a thread sleep for 1 minute, you do something like this: TimeUnit. MINUTES. sleep(1);

How do I make a specific thread sleep?

You can't make another thread sleep. You can only make the current thread sleep. The Thread can only make itself sleep. You need to pass it a signal somehow (implemented in your own code).


2 Answers

You can make your thread sleep for 30 minutes like this:

Thread.sleep(30 *   // minutes to sleep              60 *   // seconds to a minute              1000); // milliseconds to a second 

Using Thread.sleep is not inherently bad. Simply explained, it just tells the thread scheduler to preempt the thread. Thread.sleep is bad when it is incorrectly used.

  • Sleeping without releasing (shared) resources: If your thread is sleeping with an open database connection from a shared connection pool, or a large number of referenced objects in memory, other threads cannot use these resources. These resources are wasted as long as the thread sleeps.
  • Used to prevent race conditions: Sometimes you may be able to practically solve a race condition by introducing a sleep. But this is not a guaranteed way. Use a mutex. See Is there a Mutex in Java?
  • As a guaranteed timer: The sleep time of Thread.sleep is not guaranteed. It could return prematurely with an InterruptedException. Or it could oversleep.

    From documentation:

    public static void sleep(long millis) throws InterruptedException 

    Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.


You could also use, as kozla13 has shown in their comment:

TimeUnit.MINUTES.sleep(30); 
like image 67
sampathsris Avatar answered Oct 18 '22 16:10

sampathsris


The answer of Krumia already perfectly shows how to sleep a running Thread. Sometimes, the requirement to sleep or pause a thread originates from the wish to perform an operation at a later date. If that's the case, you should better use a higher level concept like Timer or ScheduledExecutorService:

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); executor.schedule(operation, 30, TimeUnit.MINUTES); 

Where operation is the Runnable you want to execute in 30 minutes.

Using a ScheduledExecutorService, you can also execute operations periodically:

// start in 10 minutes to run the operation every 30 minutes executor.scheduleAtFixedDelay(operation, 10, 30, TimeUnit.MINUTES); 
like image 32
isnot2bad Avatar answered Oct 18 '22 17:10

isnot2bad