在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) {
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,它不会让我myThread.interrupt();在我的代码中的所需位置使用,给我一个错误说"变量'myThread'可能没有被初始化"并且因此不会编译.但是,当整个线程包含在类中但它没有办法重新启动时,它可以工作.换句话说,我需要一种方法来在线程包含在方法中时中断线程.[2]
接受的解决方案
[1].可以重新启动线程的解决方案
[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();
}
}
Run Code Online (Sandbox Code Playgroud)
另一个版本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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |