【问题标题】:How to interrupt a thread by button listener如何通过按钮监听器中断线程
【发布时间】:2015-02-10 08:36:01
【问题描述】:

我有一个按钮监听器,其中包括一个线程睡眠和另一个按钮监听器。

第二个按钮监听器必须中断这个线程,我不知道该怎么做:

我的代码:

button1.setOnClickListener (new View.OnClickListener() {

        @Override
        public void onClick(View v) {


..........


                button1.setEnabled(false);
                button1.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
                button2.setEnabled(true);


                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2800);
                        } catch (InterruptedException e) {
                          // ???????
                        }

                        MainActivity.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                button1.setEnabled(true);
                                button1.setVisibility(View.VISIBLE);
                                button2.setEnabled(false);
                                button2.setVisibility(View.GONE);
                            }
                        });
                    }
                }).start();



 button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


..............


         //  Thread.interrupted(); -> does not work


                    }
                });

}   });

如何让 button2 监听器中断线程?

【问题讨论】:

  • 你试过thread.interrupt();吗?
  • 是的,我试过了,但正如 Randev 所说,不适用于匿名用户

标签: java android runnable thread-sleep interrupted-exception


【解决方案1】:
   class TestInterruptingThread1 extends Thread{  
        public void run(){  
        try{  
        Thread.sleep(1000);  
        System.out.println("task");  
        }catch(InterruptedException e){  
        throw new RuntimeException("Thread interrupted..."+e);  
        }  

        }  

    b2 //
   {
    try{  
        t1.interrupt();  // t1 is the thread to be interrupted
        }catch(Exception e){System.out.println("Exception handled "+e);}  

        }
    } 

【讨论】:

    【解决方案2】:

    不是线程的匿名实现,而是使线程成为类变量。假设线程的名字是 btnOneThread

    private Thread btnOneThread;
    

    在需要的地方初始化线程并启动它。

    btnOneThread = new Thread( ){...}
    btnOneThread.start();
    

    当button2被点击时调用

    if (btnOneThread != null) {
        btnOneThread.interrupt();
    }
    

    【讨论】:

      【解决方案3】:

      通过调用 thread.interrupt() 方法。您还需要将 Thread 实例保存在类的字段中。

      【讨论】:

        【解决方案4】:
        Thread t = new Thread() {...}
        t.start();
        

        在监听器中:

        t.interrupt();
        

        至于

        //  Thread.interrupted(); -> does not work
        

        它将在派生自 Thread 的类中工作,并且与 this.interrupted() 相同; t.interrupted() 将在类定义之外工作。

        还要注意isInterrupted()方法不会清除中断标志,而interrupted()有清除标志的副作用

        【讨论】:

          猜你喜欢
          • 2013-12-24
          • 1970-01-01
          • 1970-01-01
          • 2021-02-15
          • 1970-01-01
          • 1970-01-01
          • 2016-08-27
          • 2013-07-31
          相关资源
          最近更新 更多