安卓:包含在方法中断线程线程、方法

2023-09-07 16:04:24 作者:伪装の、快乐

在Android的工作室,我已经包含在方法的线程像这样(如下图所示),因为我想为重新启动线程时,它被称为 [1],(重新创建线程,而不是重新启动)

In Android Studio, I have a thread contained in a method like so(seen below) because I would like to restart the thread whenever it is called[1] ,(recreating the thread rather than restarting)

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) {

               }
           }
       }
   });

我的问题是,它不会让我使用 myThread.interrupt(); 在我的code所需的位置,给了我一个错误说变量 MyThread的可能没有被初始化,正因为如此将无法编译。然而,它的工作原理当整个线程被包含在类,但我没有重新启动它的方式。换句话说,我需要一种方法来中断线程,而它被包含在一个方法。 [2]

My problem is that it wont let me use myThread.interrupt(); in the desired location in my code, giving me an error saying "variable 'myThread' may not have been initialized" and will not compile because of this. However it works when the entire thread is contained in class but I do not have a way of restarting it. In other words, I need a way to interrupt the thread while it is contained in a method.[2]

接受的解决办法。

[1]。 一个解决方案,该线程可以重新启动

[2]。 其中,而线程包含在方法的线程可以中断的解决方案

PS:如果不明确的,我已经编辑我的code以上,方便的可读性,但我还没有删除的东西,如环或视频下载,因为我认为他们可能是问题的一部分。所以,如果孤单太多或太​​少某大括号的话,那不是问题。

PS: If its unclear, I have edited my code above for easy readability but I have not removed things like the for loop or the Thread.sleep as I assume they could be part of the problem. So if theres too many or too little of a certain curly bracket then thats not the problem

编辑:搜索周围,显然你不能重新启动一个线程

Searched around and apparently you cant restart a thread

推荐答案

您可以进入for循环之前启动在 MyThread的的中断线程。在中断线程休眠5秒钟,然后中断 MyThread的。中断在异常处理程序处理。有循环变量 X 重置为 0 ,这实际上是循环的重新启动。

You can start the interrupting thread in myThread before entering the for-loop. The interrupting thread sleeps 5 seconds and then interrupts myThread. The interrupt is handled in the exception handler. There the loop variable x is reset to 0, which is practically a restart of the loop.

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();
    }
}

和另一个版本,其中 MyThread的不睡觉,和同步使用而不是一个 InterruptedException的

And another version where myThread does not sleep, and synchronization is used instead of an 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();
    }
}