【问题标题】:How to stop working ScheduledExecutorService?如何停止工作 ScheduledExecutorService?
【发布时间】:2018-04-26 17:24:51
【问题描述】:

在我的项目中,我有一个图表,它可以根据我们单击“开始”或“停止”按钮而变成动画。我可以让它开始,但我不知道如何阻止它。方法 shutdownNow() 没有结果。我怎样才能做到这一点?这是我的代码

public class Animation extends JPanel{
    // initializations
    ScheduledExecutorService scheduler = 
               Executors.newScheduledThreadPool(1);

    Animation(String s){
        // initialization of chart and adding XYSeries 
        this.add(chartPanel);   
    }

    public void go() {
        scheduler.scheduleAtFixedRate( (new Runnable() {

        @Override
        public void run() {
            double first;
            l = dataset.getSeries();

            while(true) {   
                first = (double)l.get(0).getY(0);
                for (int k = 0; k < l.get(0).getItemCount(); k++) {
                    if (k + 1 < l.get(0).getItemCount()) l.get(0).updateByIndex(k, l.get(0).getY(k+1));
                    else l.get(0).updateByIndex(k, first);
                }   
            }
            }

        }),  0, 5, MILLISECONDS);

    }

    public void stop() {
        scheduler.shutdownNow();
    }

}

【问题讨论】:

  • shutdownNow 调用Thread#interrupt 所以尝试使用while (!Thread.currentThread().isInterrupted()) 而不是while(true)

标签: java swing animation executorservice scheduledexecutorservice


【解决方案1】:

根据 java 文档,shutdownNow() 的工作方式如下所示。

除了尽力停止处理之外,没有任何保证 积极执行任务。例如,典型的实现将 通过 {@link Thread#interrupt} 取消,因此任何失败的任务 响应中断可能永远不会终止。

因此,它会将中断标志设置为 true,因此您需要正确管理 InterruptedException 和/或明确检查 Thread.currentThread().isInterrupted()。您可以使用以下代码停止当前正在运行的输入线程。

while (!Thread.currentThread().isInterrupted()) {
    // your code here           
}

【讨论】:

    【解决方案2】:

    (!Thread.currentThread().isInterrupted()) 可能是一个解决方案

    但我个人会:

    • 在方法中提取运行器
    • 通过翻转布尔值停止跑步者
    • 需要时调用scheduler.shutdownNow();(关闭JPanel?)

    示例:

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    boolean running;
    
    public void setup() {
       scheduler.scheduleAtFixedRate(runner(), 0, 5, TimeUnit.MILLISECONDS);
    
    }
    
    private Runnable runner() {
      return () -> {
    
        while (running) {
           try {
            //DO YOUR STUFF HERE
            System.err.println("RUNNING");
            Thread.sleep(500);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
       }
     };
    }
    
    public void go() {
      running = true;
    }
    
    public void stop() {
     running = false ;
    }
    
    public void shutdown() {
      scheduler.shutdownNow();
    }
    
    
    public static void main(String[] args) throws InterruptedException {
      Demo tasks = new Demo();
      tasks.setup();
      for (int i = 1; i <= 5; i++) {
        System.err.println("GO FOR IT " + i);
        tasks.go();
        Thread.sleep(2000);
        tasks.stop();
        Thread.sleep(1000);
      }
      tasks.shutdown();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2011-09-26
      相关资源
      最近更新 更多