• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wait few sec

 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, can anyone show me just one example how to create something like delay?

For example:



Please show me some kind of example..

I know about Thread.sleep, but im looking for other method.

Thanks in advance
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, normally you use Thread.sleep. Why are you looking for something else? (Presumably the answer to that question is part of your actual question which didn't get posted.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote:Hello, can anyone show me just one example how to create something like delay? ...

I'm with Paul. Thread's sleep method sounds appropriate for what you've described. Is there more to this?

Are you looking for something like java.util.Timer?
A facility for threads to schedule tasks for future execution in a background thread.


 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made code in quite biiig class, and few people said that:

"Using thread.sleep is really bad idea"
 
Henry Wong
author
Marshal
Pie
Posts: 20883
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Mintalm wrote:I made code in quite biiig class, and few people said that:

"Using thread.sleep is really bad idea"



You may be taking the critic out of context. Perhaps they were saying that the technique (of sleeping) you are using is a bad idea -- not the actual implementation. If this is the case, all you are doing is doing the same thing, just differently -- which from this topic, you can conclude is also a bad idea.

If you tell us exactly what code the critic is about, maybe we can tell you why (and whether the critic is even valid).

Henry
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One unconventional way to achieve the same......see the sample code


But again..it will be processor dependent and not as reliable as Thread.sleep. But since you asked for a different way to do this, I remembered my degree class for Algorithm. this is the way we used to determine the time taken by a sample code.

This is just a basic idea ; you can change the parameter value as per your wish.


 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output for the above code was :


Thu Apr 22 17:17:32 IST 2010
waitTime(40000)
Thu Apr 22 17:17:41 IST 2010


Enjoy
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also store the initial time and then use a while loop to compare this with the current system time until 10 seconds (or whatever) had passed.

Any of these looping solutions though will use up processor for no gain, so thread.sleep() would definitely be what I would go for.
 
Mathew Mintalm
Ranch Hand
Posts: 103
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You, Dhan Kumar
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew, do you have any feedback on why Thread.sleep() was not suitable?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dhan: A JVM might optimize that loop right out of existence: you can never rely on simple looping to provide a consistent delay.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JVM might optimize that loop right out of existence: you can never rely on simple looping to provide a consistent delay.


in that case you can put some one liner statement(meaningless) in the for loop like int p = p + 1.
And you are right about relying on this method to provide a delay but this is just another way to create some delay in program and yes which is not reliable.
 
Ritesh Pareek
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Sleep will not release allocated resources for that thread... can be condition for deadlock hence it is a bad idea
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But forcing the thread to do something else in a loop for 10 seconds will also not release any locks, so is just as likely to cause deadlock, plus it is consuming processor resource as well and the actual time it takes cannot be guaranteed...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I basically agree with Doug.

When these people told you that using sleep is a "really bad idea," did they offer any justification? Did you ask them why?

Unless we know the rationale, we can't offer much, because we don't know what the true problem is. It's hard to imagine that tying up the processor with bogus iterations is a good alternative.
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Marc is right. It's been a week and we still haven't been told what the question is.
 
Ritesh Pareek
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally if we conclude... using sleep is a abd idea because:

1. Sleep will not release allocated resources for that thread.
2. Can cause a deadlock.


any more??
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's too obvious, and too easy?
 
Doug Braidwood
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh,
In this case it's not sleep that is "causing" deadlock by not releasing the resources, it's poor design.
The problem of deadlock and not releasing resources would be caused just the same by using a function designed to take 10 seconds to elapse.

Consider this pseudocode


This will do the work, then wait 10 seconds without releasing the lock.

If your alternative is a function which uses up processor resources then it is subject to exactly the same design considerations



will have exactly the same problem. It will do the work, then it will hold onto the resource, potentially causing locking issues, until the 10 seconds are up.


But if you just design it right


then it does the work, releases the lock, then sleeps 10 seconds.


In terms of locking resources and deadlock, I see no difference between the Thread.sleep() option and the methodThatDoesNothingButTakesTenSeconds() option.

In terms of performance, the methodThatDoesNothingButTakesTenSeconds() has the following drawbacks which various people have highlighted : may be optimised out by JVM; will use processor resources which could be used on real work; may run for more or less than 10 seconds depending on the processor time allocated to it.

For me Thread.sleep is clearly a better idea...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh Pareek wrote:Finally if we conclude... using sleep is a abd idea because...

No, I do not think that is a valid conclusion, for the reasons Doug outlined above.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, in case my previous reply was not clear: I too think that sleep() is the preferred way to solve this, by far, and I don't agree with Ritesh's premise.
 
Ritesh Pareek
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Doug,

Thanks, you made it clear like anything.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic