如何强制停止线程并使用新变量启动同一线程? [英] How to forcefully stop thread and start same thread with new variable?

查看:120
本文介绍了如何强制停止线程并使用新变量启动同一线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按钮单击时执行一些长时间运行的任务,如果用户再次单击该按钮,当前任务将被强制停止并执行下一个任务?

I want to do some long running task on a button click, if user click again on that button current task forcefully stopped and will do next task ?

推荐答案

我正在发布一个基于Java的纯代码,该代码也将在android中工作.使用android时,请注意不要从不是在主GUI线程之外的线程中执行的程序的一部分更新GUI.如果希望从另一个线程编辑GUI,则可以使用在主GUI线程中实例化的Handler.

I am posting a pure Java based code which will work in android as well. While working with android Just you need to take care that you do not update GUI from a part of the program which gets executed in a Thread other than Main GUI thread. If You wish to edit the GUI from another thread then you may use Handler instantiated in the main GUI thread.

定义此类模型的基本界面

Define the basic interface for such model

/**
 * 
 * @author nits.kk
 * 
 * This defines the methods for the model.
 *
 */
public interface IResumable {
  /**
   * starts the model
   */
  public void requestStart();
  /**
   * requests the model to pause
   */
  public void requestPause();
  /**
   * requests the model to resume with new parameter
   * @param newParam
   */
  public void resumeWithNewParam(int newParam);
 /**
   * terminate the model
   */
  public void requestStop();
}

现在具体实施

public class ResumableModel implements IResumable {
  private Thread worker;
  private WorkerRunnable work;

  public ResumableModel(int initialValue) {
        work = new WorkerRunnable(initialValue);
        worker = new Thread(work);
  }

  @Override
  public void requestStart() {
        worker.start();
  }

  @Override
  public void requestPause() {
        work.setPauseRequested(true);
  }

  @Override
  public void resumeWithNewParam(int newParam) {
        work.setNewParam(newParam);
  }

  @Override
  public void requestStop() {
        worker.interrupt();
  }

  private static class WorkerRunnable implements Runnable {
        private int param; // we can have the variable of the type depending upon the requirement.
        private final Object lock = new Object();
        private volatile boolean isPauseRequested = false;

        public void run() {
              synchronized (lock) {
                    try {
                          while (!Thread.currentThread().isInterrupted()) {
                                while (isPauseRequested) {
                                      lock.wait();
                                }
                                System.out.println("value of param is" + param);
                          }
                    } catch (InterruptedException e) {
                          e.printStackTrace();
                    }
              }
        }

        public WorkerRunnable(int param) {
              this.param = param;
        }

        private void setPauseRequested(boolean isPauseRequested) {
              this.isPauseRequested = isPauseRequested;
        }

        private void setNewParam(int param) {
              // double locking to prevent the calling thread from being locked
              // (if in running state without pause requested then calling thread
              // will be in indefinite wait state for acquiring the lock.
              if (isPauseRequested) {
                    synchronized (lock) {
                          if (isPauseRequested) {
                                this.param = param;
                                this.isPauseRequested = false;
                                lock.notifyAll();
                          } else {
                                // logger will be used in real application
                                System.out.println("Need to pause first before setting a new param"); 
                          }
                    }
              } else {
                    // logger will be used in real application
                    System.out.println("Need to pause first before setting a new param"); 
              }
        }
  }
}

现在,当您想启动该线程时,您可以执行以下操作

Now when you wish to start The thread you can do as below

IResumable resumable = new ResumableModel(10);
resumable.requestStart();

要暂停线程时,只需调用requestPause()方法

When you want to pause the thread you can simply invoke the requestPause() method

resumable.requestPause();

现在,当您需要使用新的变量值恢复线程时,可以调用resumeWithNewParam.

Now when you need to resume the Thread with a new variable value, you can invoke resumeWithNewParam.

resumable.resumeWithNewParam(20);

现在,当您觉得不需要线程并且模型应该终止时,您可以调用resumable.requestStop();

Now when you feel you do not need the thread and model should terminate then you can invoke resumable.requestStop();

这篇关于如何强制停止线程并使用新变量启动同一线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆