在 Android Studio 中,我有一个线程包含在像这样的方法中(如下所示),因为我想每当调用该线程时都重新启动该线程[1] ,(重新创建线程而不是重新启动)
public void callthread(){
final Thread myThread = new Thread(new Runnable()
{
@Override
public void run()
{
for (int x=0; x<750&&!Thread.interrupted(); x++) {
//using this and thread sleep for repeated timed code within a thread
try {
Thread.sleep(4);
runOnUiThread(new Runnable()
{
@Override
public void run() {
//some code
if (condition) {
myThread.interrupt();
}
}
});
} catch (InterruptedException e) {
}
}
}
});
我的问题是它不允许我使用myThread.interrupt();
在我的代码中所需的位置,给我一个错误,说“变量'myThread'可能尚未初始化”,因此不会编译。然而,当整个线程包含在类中时它会起作用,但我没有办法重新启动它。换句话说,我需要一种方法来中断线程包含在方法中的情况。[2]
接受的解决方案
[1]. A solution where the thread can be restarted
[2]. 当线程包含在方法中时可以中断线程的解决方案
PS:如果不清楚,我已经编辑了上面的代码以便于阅读,但我没有删除诸如for
循环或Thread.sleep
我认为它们可能是问题的一部分。因此,如果某个花括号太多或太少,那么这不是问题
编辑:四处搜索,显然你无法重新启动线程
您可以在中启动中断线程myThread
在进入for循环之前。中断线程休眠5秒然后中断myThread
。中断在异常处理程序中处理。那里有循环变量x
被重置为0
,这实际上是循环的重新启动。
public class ThreadInterruptRestart {
public static void main(String[] args) {
new ThreadInterruptRestart().callthread();
}
public void callthread() {
final Thread myThread = new Thread("myThread") {
@Override
public void run() {
final Thread _this = this;
Thread interruptingThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// this is not the interrupt we are interested in
e.printStackTrace();
}
if (true) {
System.out.println("interrupting " + _this + " from thread " + this);
_this.interrupt();
}
}
}, "interrupting thread");
interruptingThread.start();
for (int x = 0; x < 750; x++) {
// using this and thread sleep for repeated timed code
// within a thread
try {
System.out.println(x);
Thread.sleep(10);
} catch (InterruptedException e1) {
// this is the interrupt we want to handle
System.out.println("" + this + " interrupted!");
// reset the loop counter
x = 0;
}
}
}
};
myThread.start();
}
}
还有另一个版本myThread
不休眠,并且使用同步而不是InterruptedException
:
public class ThreadInterruptRestart {
public static void main(String[] args) {
new ThreadInterruptRestart().callthread();
}
public void callthread() {
final Object mutex = new Object();
final Thread myThread = new Thread("myThread") {
@Override
public void run() {
final Thread _this = this;
Thread interruptingThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// this is not the interrupt we are interested in
e.printStackTrace();
}
if (true) {
System.out.println("interrupting " + _this + " from thread " + this);
synchronized (mutex) {
_this.interrupt();
}
}
}
}, "interrupting thread");
interruptingThread.start();
for (int x = 0; x < 75000; x++) {
// using this and thread sleep for repeated timed code
// within a thread
synchronized (mutex) {
System.out.println(x);
// do other stuff here
}
if (Thread.interrupted()) {
// this is the interrupt we want to handle
System.out.println("" + this + " interrupted!");
// reset the loop counter
x = 0;
}
}
}
};
myThread.start();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)