aspose file tools*
The moose likes Beginning Java and the fly likes Wait few sec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wait few sec" Watch "Wait few sec" New topic
Author

Wait few sec

Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 31, 2004
Posts: 11343

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.



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

I made code in quite biiig class, and few people said that:

"Using thread.sleep is really bad idea"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

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
SCJP - Here for Knowledge..
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

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

Joined: Apr 04, 2010
Posts: 42
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.


SCJP, SCWCD
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

Thank You, Dhan Kumar
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Mathew, do you have any feedback on why Thread.sleep() was not suitable?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@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

Joined: Aug 03, 2009
Posts: 29

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

Joined: Nov 04, 2008
Posts: 50
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

Joined: Apr 04, 2010
Posts: 42
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

Joined: Aug 31, 2004
Posts: 11343

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Yes, Marc is right. It's been a week and we still haven't been told what the question is.
Ritesh Pareek
Ranch Hand

Joined: Nov 04, 2008
Posts: 50
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
It's too obvious, and too easy?
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Nov 04, 2008
Posts: 50
@Doug,

Thanks, you made it clear like anything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wait few sec