2013PMC2000
2013PMC2000

Reputation: 25

Another way of Thread.sleep with Do While Loops in Java?

Whenever I write Thread.sleep(500); in my code, it always tells me this sometimes causes problems to use Thread.sleep();. Is there another way to delay time before certain things. If so, can you give me the code and explanation. Thank you

Upvotes: 0

Views: 1883

Answers (3)

TwoThe
TwoThe

Reputation: 14259

If you want something to happen in the near future, using sleep repeatedly is a bad thing. For once it does not guarantee to be "on time", and for second it can be interrupted at any time, causing your program to malfunction.

If you have one part of your code that needs to be executed repeatedly, using a Timer can solve that perfectly. If you have multiple and different parts of code that need to be executed with given delays, you should use a ScheduledExecutorService. You can either use the ScheduledThreadPoolExecutor directly or - more conveniently - use the ExecutorService.

You can easily circumvent threading issues by just using a single thread. However threading isn't that difficult in Java if you use the right tools.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718698

Firstly, you don't show us any code and you don't mention what "it" is. (As in "it always tells me ...".) This makes a specific answer impossible.


It most likely that the problem referred to is that sleep makes your code either wasteful or unresponsive. A common "hack" used to make a thread wait for some condition is to repeatedly call sleep and then test the condition. The problem is that if the condition becomes true while you are sleeping, the thread will still be held up until the sleep interval expires. If you make the sleep interval smaller, then you "burn" more CPU cycles with wakeup / test / sleep iterations.

If you are trying to implement a "wait for some condition to become true", then the efficient way to do it is to either use wait() and notify() (or notifyAll()), or an appropriate higher level synchronization class.

The classic Java pattern is like this:

    // waiting for the condition
    synchronized (obj) {
        while (!condition) {
            obj.wait();
        }
    }

    ...

    // updating the condition ...
    synchronized (obj) {
        // do something that makes condition true
        obj.wait();
    }

Note that the use of synchronized is essential if you use wait/notify.

Upvotes: 2

Vishal Shukla
Vishal Shukla

Reputation: 2998

Condition interface can be useful to let other threads notify you about specific events and ask the current thread to get "parked" till then. Condition interface also has a methods called waitUntil(Date deadline) - which causes current thread to wait until the deadline elapses.

Are you waiting for some other thread to perform some activity and wait till then? Use any blocking data structure like BlockingQueue or other advanced synchronizers like CountdownLatch.

If just waiting for some thread to complete its execution use join().

As mentioned bay MadProgrammer and alfasin, its important to know what you are trying to achieve and what is the problem in sleep() you are talking about.

Upvotes: 0

Related Questions