Results 1 to 6 of 6
Thread: making a thread wait
- 11-14-2010, 07:20 PM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
making a thread wait
This is part of a for-loop. I want to excecute some code, but want it to wait some time each time it goes through the loop. The time it needs to wait in miliseconds each time is set in "waitTimes", which is an long[]. I read the API of "object.wait()" and saw all kinds of errors it could throw. I don't think most of it apply in my case, but I wanted to be safe. So I build this whole construct.Java Code:long timeNeededToWait=waitTimes[i]; while(timeNeededToWait>0) { long time=System.nanoTime(); try { this.wait(timeNeededToWait); } catch (InterruptedException e) { } timeNeededToWait=time-System.nanoTime()-timeNeededToWait; }
All I want it to do is make the thread wait the amount of miliseconds specified in waitTimes[i], does this code do that, or did I miss something?
- 11-14-2010, 07:48 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
I think you are looking for the sleep() method ?! :)
- 11-14-2010, 08:05 PM #3
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
Just found it in the thread class, what is the differce between the wait() methode in the object class and the sleep() methode?
- 11-14-2010, 08:07 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Read the API Doc:
Thread.sleep(long)
Object.wait()
- 11-14-2010, 08:21 PM #5
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
I did, they seem to do exactly the same. Only the wait() methode can throw more exceptions, but the sleep() methode needs to be applied on the thread, not the object. Therfore the logical choice seems to me the wait() methode.
Which brings me back to my original question: does my code do what I intend it to do?
- 11-16-2010, 06:25 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Thread wait for another thread
By jitheshmenon in forum Threads and SynchronizationReplies: 2Last Post: 08-05-2010, 02:13 PM -
Wait() Question
By rsvr in forum Threads and SynchronizationReplies: 3Last Post: 04-27-2010, 03:39 PM -
Need help with wait() and notify()
By Mkaveli in forum Threads and SynchronizationReplies: 2Last Post: 03-30-2010, 11:58 AM -
about wait() and notifyALL
By denis in forum Threads and SynchronizationReplies: 13Last Post: 04-22-2009, 08:28 AM -
Thread Wait
By jiexx in forum Threads and SynchronizationReplies: 1Last Post: 03-19-2009, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks