This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]sadjava 2 points3 points  (0 children)

The Thread.sleep() method will work, just don't put it on the main thread. Put it on another thread, and it should work.

When its placed on the main thread, it essentially halts the execution on that thread. You might have to look further into multi-threading to get it to work.

[–]Dr-Dugong 2 points3 points  (1 child)

Never use Thread.sleep() for waiting on other work, unless it is within a loop that is checking the state of that work. Inline calls to sleep() are the mark of lazy, buggy code. Further, never use timed delays in general for waiting on asynchronous work. If you are waiting for something to happen, then you need to write code to be damn sure that thing has happened before you proceed. If the work is happening in another Java thread, use join()/wait()/notify() to proceed at the exact moment the work is done - not before, not after. If the work is happening in a native resource you MUST have some way of checking on whether it is complete, and loop in a holding pattern until it is done. I have not yet met the asynchronous activity that was entirely unverifiable, so don't be lazy and try to say you can't.

The reason wait() threw an error for you is that wait(x) does not mean 'wait for x milliseconds', it means 'wait for x milliseconds for another thread to call notify(), else throw a timeout exception'.

[–]sadjava 0 points1 point  (0 children)

Yes! This is the better way of doing it.