请求线程中断不适用于Ios

时间:2019-05-27 10:04:18

标签: codenameone

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

我附上代码以尝试。

我的临时解决方案是使用Flag来中断,但我想使用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();
    }

}

1 个答案:

答案 0 :(得分:1)

iOS不支持此功能。停止也不会停止,因为很难跨平台一致地工作。对于iOS和JavaScript端口中的线程实现尤其如此。