我创建了一个新线程,y 需要中断,我使用 thread.interrup()
,但是当我抛出请求线程中断在 ios 上不起作用时,在模拟器或 Android 设备上工作正常。
我附加代码来尝试一下。
我的临时解决方案是使用标志来中断,但我想使用中断异常
package com.kandy.forms;
import com.codename1.io.Log;
import com.codename1.ui.Button;
import com.codename1.ui.Dialog;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BoxLayout;
public class Interrup extends Form {
private Form previous;
private Thread thread = null;
public Interrup() {
setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Button newThread = new Button ("Start Thread");
newThread.addActionListener((e) -> {
thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
Log.p("thread working");
} catch (InterruptedException ie) {
Dialog.show("Message", "Interruption received", "Ok", null);
break;
}
}
}
});
//thread start
thread.start();
});
Button interruptTreath = new Button ("Interrupt");
interruptTreath.addActionListener((e) -> {
Log.p("Interrupt Sended");
thread.interrupt();
});
add(newThread);
add(interruptTreath);
}
public void show() {
previous = Display.getInstance().getCurrent();
super.show();
}
public void goBack(){
previous.showBack();
}
}
这在 iOS 上不受支持。停止等也不是,因为这些很难跨平台一致地工作。对于 iOS 和 JavaScript 移植中的线程实现尤其如此。