请求线程中断在 Ios 上不工作

Request thread interrupt not working on Ios

我创建了一个新线程,你需要中断,我使用 thread.interrup(),但是当我抛出请求时,线程中断在 ios 上不起作用,在模拟器或 Android 设备.

我附上代码试试看。

我的临时解决方案是使用 Flag 来中断 while,但我想使用 InterruptedException

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 不支持此功能。 stop 等也不是,因为它们很难跨平台一致地工作。对于 iOS 和 JavaScript 端口中的线程实现尤其如此。