File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes stopping a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "stopping a thread" Watch "stopping a thread" New topic
Author

stopping a thread

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Sir,

I want to stop a thread. I'm reading "Thinking in java" which says either interrupt() or the static metthod interrupted() would help me leave run() as stop() is deprecated. i'm using it in the code but the thread is not stopping. the code is:



I don't want to use executors initially.
Can you help me.

Thanks in advance

Komal
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Too difficult a question for us beginners. Moving.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
You're misunderstanding how interrupt/interrupted works. "interrupt" would generally be called from outside the thread, while the thread would use "interrupted" to check whether "interrupt" has been called. (Note that "interrupted" doesn't do anything - it returns something, which your code currently ignores.)

java.lang.Thread


Ping & DNS - my free Android networking tools app
Manoj Maniraj
Ranch Hand

Joined: Mar 25, 2009
Posts: 38
You can stop like this


http://manojmaniraj.blogspot.com
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1821
    
    7

Ulf Dittmer wrote:Note that "interrupted" doesn't do anything - it returns something, which your code currently ignores.


Well, in addition to returning the interrupted status of the thread the interrupted() method also reset the interrupted status. So if you want to get the interrupted status without resetting it you should use isInterrupted() instead.
A subtle but important distinction.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
hi manoj,

well, your code works fine. but it exits with no result.

regards,
komal
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Komal,

Modify your code to implement finite loop instead of infiite loop. Otherwise use break statement instead of using Thread.inrerrupted()

Thanks,
Niranjan
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
I'am able to use a finite loop and stop the thread by a boolean, but the interrupt() as such is not working.
can anybody please help me.
please show how the interrupt works through some code

regards,
Komal
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
change your code to interrupt the current thread.

Thread.currentThread().interrupt();

in the catch block write the logic to break your loop.
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Thanks Sanju,

i got it. i called a condition in a seperate method and interrupted the thread there and called the method into run where i used an infinite loop. ctecking if the thread is interrupted by thread.interrupted, i used break to exit the run.
but my question still is not answered.


Dittmer had said:
"interrupt" would generally be called from outside the thread


the interrupt whether called from outside is called on the same thread. how can we call it from outside. i would be glad if you can be more elaborate.

thanking you,

with regards,
komal
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

You just keep a reference to the Thread, then call the interrupt() method on that reference.
Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
Hi hi,

I'm no expert but shouldn't the stopped variable be declared as volatile to prevent it from being cached in registers?

happy easter

Graham

Manoj Maniraj wrote:You can stop like this
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
The following is my code. As i have to call the method which calls interrupt() insie run to stop the thread. where is the question of calling it from outside.


help me please.

thanks in advance.

komal
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Komal,

Check my modifications to your code. I think it will solve your problem.



Thanks,
Niranjan
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Nope Naga, it is not working, seems like after main thread completes its sleeptime, the interrupted exception is being thrown from there. it is being interrupted in the main and the mainthread is exiting.the uaser thread is continuiing the loop after throwing an interrupted exception.

any solution anyone.

Thanks,
komal
Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18

Hi,

try putting the break statement in this try/catch. You're testing isInterrupted AFTER its been interrupted... I think...




try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
BREAK; // HERE

}


so by the time you get here...

//stop1();
if(Thread.interrupted())
{
System.out.println("Thread stoopped");
break;
}


you're no longer interrupted.

let me know if this helps

Naga Niranjan wrote:Komal,

Check my modifications to your code. I think it will solve your problem.



Thanks,
Niranjan
Maris Orbidans
Ranch Hand

Joined: Mar 08, 2004
Posts: 149
read javadoc before coding !

The interrupted status of the current thread is cleared when this exception is thrown.

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Hi Warrent,

Thanks loads,
it worked!!

I later used a boolean and flagged the loop in the run. calling the method stop1 with a thread parameter.i called this method in the main putting the stop1 in the main.get it...

boolean cancel = false;

stop1(Thread t){
cancel = true;
}
run(){
//all code here
}
public static void main()
{
//thread obj;

Thread.sleep(10000);

obj.stop1(t1)//t1 is the thread i created and obj my class object.
}

i could resolve it, BUT this is what is wanted.
Thanks loads

regards,
komal
Walsh graham
Greenhorn

Joined: Apr 10, 2009
Posts: 18
Hi Hi,

delighted your're up and running.


make that boolean flag volatile to be safe. You've nothing to lose except register storage and it could save you a lot of debugging in months to come if somebody modifies/misuses your code. If performance is a huge part of your applications requirements (and I mean HUGE ), then check before making it volatile.

have a nice day

Graham
Au Yong Jin Yoo
Greenhorn

Joined: May 13, 2013
Posts: 1
I have going to implement similar changes in my company, the headache of my situation is in the store procedure/ function have update the same table (it might have same row record but different column). I hope that you have any others advice or better solution for record lock issues ? Hope you can reply. Thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stopping a thread