Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to stop a running thread

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I just wanted to ask that suppose we have a menu and the third option is to stop the running thread, then how does it work. I tried to use stop() method , bur it doesn't work.
and can anybody guide me if we can use join() method for that.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
stop() is deprecated.
you can use

yield()

sleep(long milliseconds)
sleep(long milliseconds,long nanoseconds)
In both the cases, the thread will start running again if it is given the chance.
join() suspends the current thread until it dies.
Hope this helps,
shree
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
guchha,
Your contribution to Javaranch is very important to us.
However we do have a naming policy outlined here.
Please register again with a name which complies to this rule.
Otherwise I'm afraid you may come to the Ranch soon and find that your account has been locked.
Thank You.
[This message has been edited by Angela Poynton (edited December 08, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently there are no API methods that guarantee stopping a running thread. You will have to do this programatically, which is safer than relying on API methods since they can result in monitors being left behind in an inconsistent state.
Check out this article Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?. given under the API documentation for Thread.stop() method. This article talks about an implementation strategy that will help you safely stop a thread( using "state flags" ) .
Hope that helps( and if it does, post your code!! )
Ajith
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can not stop a thread premature, unless you use System.exit() method. If you want other thread to get the lock, you have to use wait() method. A thread is dead only when the run() method returns.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The proper way to stop a thread is to have the thread continually checking some variable and when the variable changes to some value, the thread should stop.
Thread's run method:


[This message has been edited by Thomas Paul (edited December 08, 2000).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guchha,
There is a way to stop a running thread provided you understand the following points:
1. The thread cannot be stopped normally, without it exiting the run() method of itself, by itself.
2. But, you can interrupt a thread by using interrupt() method and you also have isInterrupted() method in Thread to check whether the current thread has been interrupted.
So, for your specific case, you can interrupt the current running thread and provide an if-else structure.
The if condition would be whether
this.isInterrupted()==true
I have provided a code sample below which can direct you through the algorithm of how to stop a currently running thread.
<CODE>
class threadtest extends Thread
{
int cmdarg;
public threadtest(int i)
{
cmdarg=i;
}
public void run()
{
System.out.println("In run() method");
if(cmdarg==3)
{
this.interrupt();
if(this.isInterrupted())
{
System.out.println("Exit from the run() method");
//do whatever u wanna do b4 u exit
}
}
else
{
//check for all other cases of i using a if-else or switch
}
}
public static void main(String args[])
{
threadtest threadinstance=new threadtest(Integer.parseInt(args[0]));
threadinstance.start();
try
{
threadinstance.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception caught in join()");
}
if(threadinstance.isAlive())
System.out.println("threadinstance, still alive!!!");//hopefully not executed!
else
System.out.println("threadinstance is dead!!");
System.out.println("Over");
}
}
</CODE>
In the above code, you safely exit out of the run() method with the check of a condition and do the remaining checks in your else part of the code of run() method. But, one more point to remember is that, the parent thread of this thread should wait for it to complete, hence, you MUST provide the
threadinstance.join()
so that you don't get unexpected results.
After you compile, execute the code with
java threadtest 3
you can get your expected results.
Hope it helps.
Aparna
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic