24

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

sampathsris
  • 21,564
  • 12
  • 71
  • 98
venkata lokesh
  • 269
  • 1
  • 2
  • 3
  • 1
    Yes, there are tons of problems, because your thread will do nothing for 30 minutes. `Thread.sleep(1000 * 60 * 30);` , and wrap it in a try-catch – Kon Jul 22 '14 at 05:05
  • 5
    instead of sleeping a thread try using timer. – Rod_Algonquin Jul 22 '14 at 05:06
  • What are you trying to accomplish? Is this just to play around with threads? – chrylis -cautiouslyoptimistic- Jul 22 '14 at 05:06
  • Right. What's the application here, a timer may indeed be what you need. – Osmium USA Jul 22 '14 at 05:07
  • 1
    @Kon - Why would a sleeping thread create "tons of problems"? – Ted Hopp Jul 22 '14 at 05:07
  • One problem is that testing your program will get awfully boring. Make sure you have plenty of reading material. :) – ajb Jul 22 '14 at 05:10
  • 2
    @TedHopp In my opinion, it's a mark of a bad design. It may not cause problems in the execution of the code, but it's almost always better to use a Timer or something similar to achieve your end-goal. – Kon Jul 22 '14 at 05:11
  • can anyone suggest me to how to use a timer with an example program or any reference link – venkata lokesh Jul 22 '14 at 05:50
  • 1
    The second part of the question is unanswerable. What would count as a "problem"? – Raedwald Jul 22 '14 at 06:52
  • The first part of the question is surely a duplicate question. – Raedwald Jul 22 '14 at 06:53
  • 1
    @Kon - A `Timer` is nothing more than a utility built on top of a background thread. I fail to see why it's so much better than using a raw `Thread`. Just like a `Thread`, a `Timer` by default does not use a daemon thread, so (like a `Thread`) it can prevent the application from terminating until the last task is run. Just like `Thread.sleep()`, a `Timer` also does not offer real-time guarantees. The only clear benefit of a `Timer` over a `Thread` is in scheduling multiple tasks, but that doesn't seem to apply here. (But who knows what OP is actually trying to accomplish.) – Ted Hopp Jul 22 '14 at 14:57

2 Answers2

44

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);
sampathsris
  • 21,564
  • 12
  • 71
  • 98
10

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);
isnot2bad
  • 24,105
  • 2
  • 29
  • 50