Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to make simple sleep()  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I have simple method that I like to execute several times but from execution to execution
I tried with something like that without much lock


what im doing wrong here , and why is stop() deprecated? what is the alternative?
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Only way a thread can be stopped is by returning from the run() method.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void getName(int i, String s)
{
if i == 1;
return;

Thread.CurrentThread().sleep(5000);

return getName(i-1,s);
}

this should help...
not sure about the void though..

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that what you wanted to do?... just run the method three times?

Justin
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes 3 times
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okok sorry sorry
this is not so simple I guess now when I look on my case
this is the full scenario:

I have synchronized method that is calling to other private method
That need to return some objected. Basically I need to to this 3 times calls
to check if server is alive if it alive I need to stop the sleep even if it didn't complete the 3 times loop
so my pseudo code looks like this :
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make a thread sleep you can use the static method in Thread class.stop() is depricated a good reason of this has been give in the javadoc itself.

So if you want to make use to stop and resume methods in Thread then you can go for the below mentioned for stopping and resuming.
put a boolean varialbe isStop . The code which you put in the run , put that within a while(true) block and the subsequent code in
while(!isStop){
So that the code would run in case isStop is false.Write another method stopThread which would set isStop varialbe to true and write another method resumeThread which would set isStop to false.

That is it..you are not free from the deprecate stop and resume methods of the thread class.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more observation I forgot to mention in my previous post..

Thread.currentThread().sleep(5000);

sleep is a static method you do not need to give Thread.currentThread()
simply Thread.sleep(5000) is enough.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the fast reply
Rahul Bhattacharjee can you please post some simple example code ?
thanks again
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked in the API for the javadoc which Rajul Bhatterjee referred to? Here, then look for "Thread" in the lower left panel. Not only does it explain why Thread.stop() is deprecated, it also gives suggestions for alternatives, in the links provided in the destroy() resume() stop() and suspend() methods.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get some codeing example from

http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.html

Infact whatever I have suggested for coding for is issue has been covered in this article.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meir,

Can you explain, in as much detail as possible, what you're really trying to achieve? There's an awful lot of strange advice in this thread, and I fear that you're being helped down a bad path. If you explain what you're really trying to accomplish, I bet we can help you figure out the right way to do it.

Note that calling "stop()" on the main thread in a program is going to quit the whole program, exiting back to the operating system. Is that what you're trying to do?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!