简体   繁体   English

我无法完成线程(runnable),它是怎么回事?

[英]I can't finish thread (runnable), how it is?

Thread thread;     

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yippi);  
final Handler hn=new Handler();
final TextView text=(TextView)findViewById(R.id.TextView01);
final Runnable r = new Runnable()
{
    public void run() 
    {
        text.settext("hi");
    }
};
thread = new Thread()
{

    @Override
    public void run() {
        try {
            while(true) {
                sleep(1750);
                hn.post(r);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
    thread.start();
thread.stop();}

The code here. 这里的代码。 I can not stop the runnable thread. 我无法阻止可运行的线程。 Also, thread.stop() and thread.destroy() are deprecated. 另外,不推荐使用thread.stop()thread.destroy() Can somebody help me? 有人能帮助我吗? And also I don't understand how to stop the thread with the thread.interrupt() method. 而且我也不明白如何使用thread.interrupt()方法停止线程。 What's wrong? 怎么了?

The JavaDoc for Thread.stop() lists the following article as explanation for why stop() is deprecated: http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html JavaDoc for Thread.stop()列出了以下文章作为不推荐使用stop()的原因的解释: http//docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. stop的大多数用法应该由代码修改,该代码只是修改某个变量以指示目标线程应该停止运行。 The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. 目标线程应该定期检查此变量,并且如果变量指示它将停止运行,则以有序的方式从其run方法返回。 To ensure prompt communication of the stop-request, the variable must be volatile (or access to the variable must be synchronized). 为了确保快速通信停止请求,变量必须是易失性的(或者必须同步对变量的访问)。

interrupt() is more suitable to stop some Thread from waiting for something, that is probably not coming anymore. interrupt()更适合阻止某些Thread等待某些东西,这可能不会再出现了。 If you want to end the thread, it's best to let its run() method return. 如果要结束线程,最好让其run()方法返回。

创建一个布尔变量来停止线程并在while(boolean)而不是while(true)使用它。

You can use Thread.interrupt() to trigger the InterruptedException within your thread. 您可以使用Thread.interrupt()来触发线程中的InterruptedException。 I've added code below that demonstrates the behavior. 我在下面添加了演示行为的代码。 The mainThread is where your code would be and the timer Thread is just used to demonstrate delayed triggering of the interrupt. mainThread是您的代码所在,而计时器Thread仅用于演示延迟触发中断。

public class Test {

    public static void main(String[] args) {

        final Thread mainThread = new Thread() {

            @Override
            public void run() {
                boolean continueExecution = true;

                while (continueExecution) {
                    try {
                        sleep(100);
                        System.out.println("Executing");
                    } catch (InterruptedException e) {
                        continueExecution = false;
                    }
                }

            }
        };

        mainThread.start();

        Thread timer = new Thread() {
            @Override
            public void run() {

                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("Stopping recurring execution");
                mainThread.interrupt();

            }
        };
        timer.start();
    }
}

You can use interrupt method of Thread to try stop a thread, like below code. 您可以使用Thread的中断方法来尝试停止一个线程,如下面的代码。 May be it`s useful to you. 可能对你有用。

public class InterruptThread {

    public static void main(String args[]){

      Thread thread = new Thread()
      {

        @Override
        public void run() {
            try {
                while(true) {
                    System.out.println("Thread is Runing......");
                    sleep(1000);
                }
            } catch (InterruptedException e) {
                // restore interrupted status
                System.out.println("Thread is interrupting");
                Thread.currentThread().interrupt();
            }
        }
       };
      thread.start();

      try {
          Thread.sleep(5000);
      } catch (InterruptedException e) {
          e.printStackTrace();
     }

     System.out.println("Will Interrupt thread");
     thread.interrupt();
  }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM