469,287 Members | 2,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

stopping a code for 2 seconds and resume

hsn
237 100+
hello everyone.
my question is simple. how can i make my code while it is executing to stop for 2 or 3 seconds and then resume executing.

thanks in advance
hsn
Jul 23 '08 #1
12 6995
r035198x
13,262 8TB
Thread.sleep. .
Jul 23 '08 #2
hsn
237 100+
Thread.sleep. .
is there any other way rather than using threads??
Jul 23 '08 #3
r035198x
13,262 8TB
is there any other way rather than using threads??
You can never run away from threads. There is always at least one thread executing anyway. What's wrong with using Thread.sleep? It's very easy to use too ...
Jul 23 '08 #4
Nepomuk
3,112 Expert 2GB
is there any other way rather than using threads??
You could save the time with System.currentMillies() (if I recall correctly) in a long variable when you start and then run a loop which checks, if the 2 seconds have passed yet over and over again. If so, the loop ends. This is not a nice solution and will use a great deal of processing power, but it will work without the obvious mentioning of Threads.

Of course, you could always just use sleep as suggested by r0.

Greetings,
Nepomuk
Jul 23 '08 #5
chaarmann
785 Expert 512MB
is there any other way rather than using threads??
there are a lot of other ways possible, but maybe you don't like them either:

1.) run your code in a debugger and then stop it, wait for 3 seconds and then resume.
2.) produce a hardware failure which freezes your whole Windows for a few seconds. A good way to do this is to use wrong network card or USB drivers. I like hardware interrupts!
3.) make a second program and let it run for 3 seconds with higher priority than your old program. Hint: If you don't have the time to program (you could just use the nonsense loop mentioned earlier in this forum topic), just let some existing viruses run and do the job for you.
You also could synchronize with Hotmail in your Outlook to freeze your computer for some time.
4.) Accelerate your computer to light speed for 3 seconds.
Jul 23 '08 #6
r035198x
13,262 8TB
...
4.) Accelerate your computer to light speed for 3 seconds.
* giggles *
Jul 23 '08 #7
Nepomuk
3,112 Expert 2GB
1.) run your code in a debugger and then stop it, wait for 3 seconds and then resume.
Ah, but the OP want's the code to pause for 2 seconds, not 3. So that is obviously no option. ;-)
2.) produce a hardware failure which freezes your whole Windows for a few seconds. A good way to do this is to use wrong network card or USB drivers. I like hardware interrupts!
Remember, that the OP want's the code to resume - you never know, if Windows will do that. Plus, it wouldn't be platform independent. :-D
3.) make a second program and let it run for 3 seconds with higher priority than your old program. Hint: If you don't have the time to program (you could just use the nonsense loop mentioned earlier in this forum topic), just let some existing viruses run and do the job for you.
You also could synchronize with Hotmail in your Outlook to freeze your computer for some time.
Using a program not written yourself can be be very unreliable in such cases - just imagine, the virus could have unwanted side effects! (Well, so could Outlook - you could download Spam!)
4.) Accelerate your computer to light speed for 3 seconds.
I'm assuming, that the energy consumption would be slightly of the limits. Oh, and in the process of accelerating the computer to the speed of light, the pressure would become so high, that the computer would easily be squeezed into a strange unidentifiable piece of mass. Therefore, the calculation would probably not continue after deceleration.

So probably, Thread.sleep is the most viable option. ;-)

Greetings,
Nepomuk
Jul 23 '08 #8
JosAH
11,448 Expert 8TB
Note that the Thread.sleep() method has nothing much to do with threads; it just
happens to be a static method in that class.

kind regards,

Jos
Jul 23 '08 #9
hsn
237 100+
thanks guys for your help

regards
hsn
Jul 23 '08 #10
tharden3
916 512MB
did you try asking it nicely? Playing easy listening music?
Jul 23 '08 #11
chaarmann
785 Expert 512MB
Ah, but the OP want's the code to pause for 2 seconds, not 3. So that is obviously no option. ;-)
If you want it so accurate, then I must admit that it is nearly impossible to stop for 2 seconds exactly at all. The timer inside the computer is not precise enough compared to an atomic clock. And even if you increase the precision of your clock more and more, you will never hit 2 seconds exactly. But there is an infinitesimal chance that if you stop, you really could do it after exactly 2 seconds. But you never know afterwards if you did it or not (Greetings from Heisenberg, uncertainty principle).
Remember, that the OP want's the code to resume - you never know, if Windows will do that. Plus, it wouldn't be platform independent. :-D
I can give him my old USB stick. If you stick that into your PC, it will automatically install a driver that will freeze your computer for roughly 2 seconds whenever you stick in any other USB stick afterwards. It is proven and some "friends" who always borrow USB sticks instead of buying their own can prove that fact (and remember it well, because they had to reinstall WIndows to get rid of the delay).
And I still remember the extra hardware with which you could freeze a C64 while running a game. Then you could save its whole memory on a floppy. (which was very convenient to bypass the copy protection).
Using a program not written yourself can be be very unreliable in such cases - just imagine, the virus could have unwanted side effects! (Well, so could Outlook - you could download Spam!)
Well, it's easy if you have Windows 95. It's very reliable there. If you ever watched movies on your computer and suddenly your movie keeps on freezing for a half a second after every 3 seconds or so, then you know you caught one again. Watching movies there was the best and reliable virus scanner I ever had.
Always think positive, turn unwanted side effects to your advantage. It's not a bug, it's a feature!
I'm assuming, that the energy consumption would be slightly of the limits. Oh, and in the process of accelerating the computer to the speed of light, the pressure would become so high, that the computer would easily be squeezed into a strange unidentifiable piece of mass. Therefore, the calculation would probably not continue after deceleration.
You are right.So it's better to keep your computer at low speed and turn back your computer clock afterwards manually, but don't tell anyone, (This is what I always do if the evaluation period of some software ran out)
So probably, Thread.sleep is the most viable option. ;-)

Greetings,
Nepomuk
Speaking of sleep, my wife just called me the third time to go to bed . So if your computer evolves in a future day and starts marrying a female one, then going to sleep for some time cannot be denied anymore by any thread.

Good night,
Chris
Jul 23 '08 #12
Nepomuk
3,112 Expert 2GB
If you want it so accurate, then I must admit that it is nearly impossible to stop for 2 seconds exactly at all. [...] But you never know afterwards if you did it or not (Greetings from Heisenberg, uncertainty principle).
Damn. Well, at least nobody else will be able to prove, that it wasn't exactly 2 seconds. ;-)
I can give him my old USB stick. If you stick that into your PC, it will automatically install a driver that will freeze your computer for roughly 2 seconds whenever you stick in any other USB stick afterwards.
Doesn't solve the "platform independence issue" however! :-D
And I still remember the extra hardware with which you could freeze a C64 while running a game. Then you could save its whole memory on a floppy. (which was very convenient to bypass the copy protection).
Very, very naughty! ;-)
Well, it's easy if you have Windows 95. [...] Watching movies there was the best and reliable virus scanner I ever had.
:-D Hopefully however, the OP isn't using Windows 95.
Always think positive, turn unwanted side effects to your advantage. It's not a bug, it's a feature!
Well, it is a feature - a feature, the programmer of the virus probably intended. But you don't like all features of all programs, do you?
You are right.So it's better to keep your computer at low speed and turn back your computer clock afterwards manually, but don't tell anyone, (This is what I always do if the evaluation period of some software ran out)
Again: Naughty, naughty!
Speaking of sleep, my wife just called me the third time to go to bed . So if your computer evolves in a future day and starts marrying a female one, then going to sleep for some time cannot be denied anymore by any thread.
I hear, Windows 7 should have this feature! ;-)

Greetings,
Nepomuk
Jul 24 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Keith Langer | last post: by
9 posts views Thread by Joe Rigley | last post: by
6 posts views Thread by D | last post: by
8 posts views Thread by darnnews | last post: by
1 post views Thread by mclaugb | last post: by
4 posts views Thread by bjm | last post: by
2 posts views Thread by Steve | last post: by
10 posts views Thread by =?Utf-8?B?R3JlZw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.