简体   繁体   English

ScheduledExecutorService关闭不能与bufferedReader一起使用?

[英]ScheduledExecutorService shutdown not working with bufferedreader?

I am using Java 8. I have a thread need start at a specific time and end at a specific time. 我正在使用Java8。我有一个线程需要在特定时间开始,并在特定时间结束。 I am using ScheduledExecutorService to do the job: 我正在使用ScheduledExecutorService来完成这项工作:

I have two threads one name is "worker" and the other is "stopworker". 我有两个线程,一个叫“ worker”,另一个叫“ stopworker”。 The Worker class contains the actual code I need run at the specific time, the "stopworker" thread just shuts down the scheduler. Worker类包含我需要在特定时间运行的实际代码,“ stopworker”线程只是关闭了调度程序。

 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
 scheduler.schedule(worker, startSeconds, TimeUnit.SECONDS);
 scheduler.schedule(stopWorker, endSeconds, TimeUnit.SECONDS);

My Worker class: 我的Worker班:

class Worker implements Runnable {
       @Override
       public void run() {
           try {
           // HTTP GET method to call and REST API and get Streaming
           // data back, it will keep alive as long as possible and
           // gets lots of data back
           ...
           BufferedReader reader = new BufferedReader(
                   new InputStreamReader(response.getStream()));
           // deal with the data   
           ...
           } catch (IOException e) {

           } finally {
                   reader.close();
           }
       }
}

My StopWorker class: 我的StopWorker课程:

class StopWorker implements Runnable {
      @Override
      public void run() {
           try {
              scheduler.shutdownNow();
           } catch (Exception e) {
              e.printStackTrace();
           }
      }
}

My code can start at the time I want, however, it's not shutdown correctly. 我的代码可以在我想要的时间启动,但是它没有正确关闭。 I have fiddle this code a little bit and found this is because the BufferedReader class I used. 我稍微弄弄了这段代码,发现这是因为我使用了BufferedReader类。

If I replaced the "worker" thread into a simple thread sleep statement and my "stopworker" works! 如果我将“ worker”线程替换为简单的线程sleep语句,那么我的“ stopworker”将起作用! That's why I think I/O part might block my shutdown. 这就是为什么我认为I / O部分可能会阻止我的关机的原因。

The only way I shutdown the thread is by making my BufferedReader variable global and close it manually in StopWorker class. 我关闭线程的唯一方法是使BufferedReader变量成为全局变量,然后在StopWorker类中手动将其关闭。 However, the code will be ugly and an IOException will be throw in the code. 但是,代码很丑陋,并且会在代码中抛出IOException

My guessing is the shutdownNow function is still waiting the I/O finish first, am I right? 我的猜测是shutdownNow函数仍在首先等待I / O完成,对吗? Is there a good way solve this elegantly? 有什么好办法优雅地解决这个问题吗? Or is there a better way to make a thread start at a specific time and end at a specific time? 还是有更好的方法使线程在特定时间开始并在特定时间结束?

schedule() returns a ScheduledFuture , which has a boolean cancel(boolean mayInterruptIfRunning) method. schedule()返回ScheduledFuture ,它具有boolean cancel(boolean mayInterruptIfRunning)方法。 If the input stream is closable by thread interruption then just call it with true. 如果输入流可以通过线程中断关闭,则只需使用true对其进行调用。

If the stream is not interruptible then you need to close it explicitly. 如果流不可中断,则需要显式关闭它。

I have figure this out after reading all your suggestions. 阅读您所有的建议后,我已经弄清楚了。 Thank for your help, especially for the8472. 感谢您的帮助,尤其是8472。 I have changed following: 1) Change ScheduledExecutorService to ScheduledThreadPoolExecutor. 我进行了以下更改:1)将ScheduledExecutorService更改为ScheduledThreadPoolExecutor。 For some reasons, ScheduledFuture cancel doesn't work with ScheduledExecutorService in my code and I don't know why. 由于某些原因,ScheduledFuture cancel不适用于我的代码中的ScheduledExecutorService,而且我也不知道为什么。 2) Manually close the BufferedReader when canceling the class. 2)取消类时,手动关闭BufferedReader。

Hope this will help someone has the similar problems. 希望这可以帮助有人遇到类似的问题。

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

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