【问题标题】:How to stop this thread?如何停止这个线程?
【发布时间】:2016-12-22 09:12:34
【问题描述】:

我在我的 android 代码中实现了 this

我在运行方法中做了如下改动(替换为“true”):

@Override
    public void run() {
        while (!isInterrupted()) {
            try {
                // A blocking operation. Initiate a ChatManager instance when
                // there is a new connection
                pool.execute(new ChatManager(socket.accept(), handler));
                Log.d(TAG, "Launching the I/O handler");
            } catch (IOException e) {
                try {
                    if (socket != null && !socket.isClosed())
                        socket.close();
                } catch (IOException ioe) {
                }
                e.printStackTrace();
                pool.shutdownNow();
                break;
            }
        }
    }

我想在关闭应用程序之前停止该线程。所以我实现了 threadName.interrupt();方法。但这不会中断线程。

我实际上对线程池执行器的使用感到困惑。所以我不确定如何有效地做到这一点。如何实现中断该线程?当调用中断方法时,我想关闭套接字,关闭池并停止线程。

【问题讨论】:

    标签: android multithreading sockets threadpool


    【解决方案1】:

    只需调用shutDownNow 关闭池并尝试中断其中的所有线程。你可以在这个post查看区别:

    • shutdown() 只会告诉执行器服务它不能接受新任务,但是已经提交的任务会继续运行
    • shutdownNow() 会做同样的事情,并且会尝试通过中断相关线程来取消已经提交的任务。请注意,如果 您的任务忽略中断,shutdownNow 将完全正常 与关机方式相同。

    如果您想中断或取消特定线程。我建议你使用submitCallables,这样你就可以使用你的Future 对象,然后如果想取消你已经提供了执行器服务的任务,你可以调用cancel(true)在其关联的Future 上。当您的任务检测到中断请求时,它应该通过调用Thread.currentThread().interrupt() 来保持中断状态。

    【讨论】:

      【解决方案2】:

      如果你想关闭一个Android线程,你可以设置一个变量来控制run(),因为run()是结束,线程将被关闭。

      代码是这样的:

      final boolean istrue=true;
          new Thread(new Runnable() {
              @Override
              public void run() {
                  while (istrue){
                      //TODO  your work
                  }
      
              }
          }).start();
      }
      

      如果要关闭线程,只设置istrue=false

      【讨论】:

      • 我建议你称它为isRunning 或类似的名称。变量的名称应表明其用途。
      • 感谢您的建议,Barth。就按照我的代码风格来做吧!
      【解决方案3】:
      Thread thread = new Thread () {
      boolean isRunning = true;
      
      public void stopThread() {
          isRunning = false;
      }
      
      @Override
      public void run() {
          while (isRunning) {
              try {
                  // A blocking operation. Initiate a ChatManager instance when
                  // there is a new connection
                  pool.execute(new ChatManager(socket.accept(), handler));
                  Log.d(TAG, "Launching the I/O handler");
              } catch (IOException e) {
                  try {
                      if (socket != null && !socket.isClosed())
                          socket.close();
                  } catch (IOException ioe) {
                  }
                  e.printStackTrace();
                  pool.shutdownNow();
                  break;
                }
              }
          }
      };
      thread.start();
      

      试试这个代码。并在您希望线程停止时调用 thread.stopThread()。

      【讨论】:

      • 如果Threadsocket.accept() 中被阻止,仍然不会退出
      猜你喜欢
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多