I want to stop the current thread immediately. Here is my code:
Grade A:
public class A() {
public void methodA() {
For (int n=0;n<100;n++) {
}
finishingMethod();
}
}
Grade B:
public class B() {
public void runEverything() {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
A a = new A();
a.methodA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
My problem is that I need to be able to stop the thread in class B before the thread finishes. I tried the interrupt () method, but that does not stop my thread. I also heard about using a shared variable as a signal to stop my thread, but I think that with a long recursive and for-loop in my process, shared-variable will not be effective.
Any idea? Thanks in advance.
source
share