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
12 7104 Thread.sleep. .
is there any other way rather than using threads??
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 ...
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
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.
...
4.) Accelerate your computer to light speed for 3 seconds.
* giggles *
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
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
thanks guys for your help
regards
hsn
did you try asking it nicely? Playing easy listening music?
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
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
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
| | | | | | | | | | | |