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: 32599
4
posted
0
Too difficult a question for us beginners. Moving.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35222
7
posted
0
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.)
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
posted
0
hi manoj,
well, your code works fine. but it exits with no result.
regards,
komal
Naga Niranjan
Greenhorn
Joined: Aug 02, 2005
Posts: 25
posted
0
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
posted
0
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
posted
0
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.