Results 1 to 6 of 6
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default making a thread wait

    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;
    }
    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.

    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?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    I think you are looking for the sleep() method ?! :)

  3. #3
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Just found it in the thread class, what is the differce between the wait() methode in the object class and the sleep() methode?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Read the API Doc:

    Thread.sleep(long)

    Object.wait()

  5. #5
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    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?

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    No!
    The main task of this method is: "Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object" in conjunction with synchronisation and monitoring.

    An alternative would be to use a timer!

Similar Threads

  1. Thread wait for another thread
    By jitheshmenon in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-05-2010, 02:13 PM
  2. Wait() Question
    By rsvr in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-27-2010, 03:39 PM
  3. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 11:58 AM
  4. about wait() and notifyALL
    By denis in forum Threads and Synchronization
    Replies: 13
    Last Post: 04-22-2009, 08:28 AM
  5. Thread Wait
    By jiexx in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •