简体   繁体   English

如何从ScheduledExecutorService取回数据

[英]How to get data back from a ScheduledExecutorService

I have a simple example where I use a ScheduledExecutorService and run tasked delayed. 我有一个简单的示例,其中我使用ScheduledExecutorService并延迟运行任务。 I would like to know if this is a good way to the data back from my camera object. 我想知道这是否是从我的相机对象返回数据的好方法。

public class App {

  private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  private final Camera camera = new Camera();

  private Object lastPicture;

  public static void main(String[] args) {
    App app = new App();
    app.takePicture();

    // getLatestPicture when I need it on my frontend, in the future. (so image that this part can get called anytime).
    // I also want to check if this picture is not the same as the last. (I might call getLastPicture multiple times within the second.)
    Object currentPicture = app.getLastPicture();
    if (lastPicture == currentPicture) {
      System.out.println("Same picture");
    }
    System.out.println(currentPicture);
  }

  private void takePicture() {
    executorService
        .scheduleWithFixedDelay(takePictureTask(), 0, 1000, TimeUnit.MILLISECONDS);
  }

  private Runnable takePictureTask() {
    return () -> camera.takePicture();
  }

  public Object getLatestPicture() {
    return camera.getPicture();
  }

}

Camera : 摄影机

public class Camera {

  private Object picture;

  public void takePicture() {
    System.out.println("Taking picture...");

    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    picture = new Object();

    System.out.println("Finished taking picture.");
  }

  public Object getPicture() {
    return picture;
  }

}

I would make Camera feed a BlockingQueue - probably therefore making it a MovieCamera . 我会让Camera Feed成为BlockingQueue可能因此使其成为MovieCamera

    private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    private final BlockingQueue<Object> pictures = new ArrayBlockingQueue<>();
    private final Camera camera = new Camera(pictures);

You can then feed the queue rather than just storing the picture. 然后,您可以输入队列,而不仅仅是存储图片。

public static class Camera {
    private final BlockingQueue<Object> pictureQueue;

    public Camera(BlockingQueue<Object> pictureQueue) {
        this.pictureQueue = pictureQueue;
    }

    public void takePicture() {
        System.out.println("Taking picture...");

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        pictureQueue.put(new Object());

        System.out.println("Finished taking picture.");
    }

}

And you have full flexibility in handling the queue. 而且您在处理队列方面具有完全的灵活性。 You can get the latest picture by polling the queue until it is empty and know if there have been more pictures automatically. 您可以通过轮询队列直到其为空来获取最新图片,并知道是否自动有更多图片。

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

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