Stopping a thread

Hi!!!

I have a serious problem with a program i've made.More exactly i have a problem with just a part of the code because i can't stop a thread.This thread is executing a cpu intensive code(i mean the cpu is at 100% when runs this part of code), and i don't know how to stop it.It can be stopped with deprecated method stop () but even when i try to stop it with that the program still executes that part of code and then it stops the sthread.What other possibilities do i have to stop that thread?I just to add that i have to stop it when it is in the middle of executing the cpu intensive part of code.Any help is appreciated!By!!!!

[651 byte] By [Zoliqa] at [2007-10-1 23:31:05]
# 1
Have you tried Thread.interrupt()?
prometheuzza at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...
# 2
And the story continues here: http://forum.java.sun.com/thread.jspa?threadID=662264&tstart=0
prometheuzza at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...
# 3

Yes i've tried every method including Thread.interrupt ().This method really interrupts that thread but then it resumes.So in that method i use to do something else when this thread runs, first i've used interrupt () wich interrupted the thread,then then the method does something else but as the method ended that thread started to execute that cpu intensive code wich i don't want to do when i call this method. Is any other way to resolve this situation?For example i think u've played chess with the computer and when u move then the cpu starts calculating the step he will do next, and then if u want to take back your move while the cpu is thinking then when push that button your piece gets back to that position from where it was taken.So in this situation

when u push the button the thread wich is executing the algorithm stops immediatly and another thread takes your move back. In my case i have the same problem.So if u have any suggestions please help!!!Thanks in advance!!!

Zolqa at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...
# 4

I think the best way is to use a boolean flag to decide wether the thread should continue or not. Then you can choose the line(s) the thread can stop at, so it doesn't stop just before storing an important value.

private boolean running = true;

private int anImportantValue;

public void run() {

running = true;

doSomething(10);

}

void doSomenthing(int n) {

for(int i=0; i<n; i++) {

doSomething(i);

anImportantValue = i;

if(!running) break;

}

}

void stopRunning() {

running = false;

}

>

perkelea at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...
# 5

Thanks, man! I suppose that this was the only possibility to resolve this problem.It was little difficult to realize the idea but finally i've done it and

now my takeBackMove () method works.Have you got az email address? I'll send you my program when it will be ready if u want of course.Thanks again!!!By!

Zoliqa at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...
# 6
No problem! You can send it to murobbs@luukku.com so we'll see if it can beat my chess program...
perkelea at 2007-7-15 14:13:49 > top of Java-index,Other Topics,Algorithms...