Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stopping a thread

 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners. Moving.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manoj Maniraj
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can stop like this
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi manoj,

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

regards,
komal
 
Naga Niranjan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just keep a reference to the Thread, then call the interrupt() method on that reference.
 
Walsh graham
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Komal,

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



Thanks,
Niranjan
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read javadoc before coding !

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

 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic