我想立即停止正在运行的线程.这是我的代码:
A类:
public class A() {
public void methodA() {
For (int n=0;n<100;n++) {
//Do something recursive
}
//Another for-loop here
//A resursive method here
//Another for-loop here
finishingMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我需要能够在线程完成之前在B类中停止线程.我已经尝试过interrupt()方法,但这并没有阻止我的线程.我也听说过使用共享变量作为一个信号来阻止我的线程,但我认为在我的进程中使用long recursive和for循环,共享变量将无效.
任何的想法 ?提前致谢.
我很抱歉,如果我的问题听起来很愚蠢,因为我的数据结构理解不是很好.
我一直在阅读Knuth的Dancing Links算法,并且非常了解它基本上是如何工作的.提到跳舞链接的数据结构可视化看起来像一个包含列和行的表,每个单元格连接到它们的上,下,左和右单元格.我还读到在这个算法中使用循环双链表.
我想知道的是,如何将双链表放入带有列和行的表中?
据我所知,大多数双链表只有2个指针(向上和向下),这是否意味着我必须制作我自己的自定义链表,它有4个指针(向上,向下,向左和向右)?或者还有其他方法吗?
提前致谢.