0
我遇到问题,即用于终止线程的join
方法未执行在线程中启动的方法的其余部分。下面是一个代码示例:在Thread.join()执行后执行相同方法中的代码
private static Thread thread;
public static void addMessage(final String s) {
thread = new Thread() {
@Override
public void run() {
String data = Message.send(s);
addMessageToContainer(data);
}
};
thread.start();
}
public static void addMessageToContainer(String data) {
//Do some stuff with the data
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//This code here will not be executed.
}
所以通常情况下,我当然之前我所说的join
函数执行代码。但是我必须在这个线程执行后加载一些带有内容的webview。所以,当我删除join
,它将给我以下错误信息:
java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Thread-9072'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {5ac9b39} called on null, FYI main Looper is Looper (main, tid 1) {5ac9b39})
所以我能做些什么的线程执行后加载的内容?
return或'throw ...' –
更正确的是:没有办法直接杀死一个线程,因为它不可能安全地这样做(除了在进程终止时操作系统执行它)。 – ThingyWotsit