Java Executors separate tasks

Description

Java Executors separate tasks

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

class TaskProcessor implements Runnable {
  private CompletionService<String> service;
  private boolean end = false;

  public TaskProcessor(CompletionService<String> service) {
    this.service = service;
  }//w  w  w .j ava  2 s.  c om
  @Override
  public void run() {
    while (!end) {
      try {
        Future<String> result = service.poll(20, TimeUnit.SECONDS);
        if (result != null) {
          String report = result.get();
          System.out.println("ReportReceiver: Report Recived: "+ report);
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }

    System.out.println("ReportSender: End\n");
  }

  public void setEnd(boolean end) {
    this.end = end;
  }

}

class TaskRequest implements Runnable {

  private String name;

  private CompletionService<String> service;

  public TaskRequest(String name, CompletionService<String> service) {
    this.name = name;
    this.service = service;
  }

  @Override
  public void run() {
    
    service.submit(new DataGenerator(name, "Data"));
  }

}

class DataGenerator implements Callable<String> {
  private String sender;
  private String title;
  public DataGenerator(String sender, String title) {
    this.sender = sender;
    this.title = title;
  }

  @Override
  public String call() throws Exception {
    try {
      Long duration = (long) (Math.random() * 10);
      System.out.println("Generator: "+ this.sender+" "+ this.title+" "+duration);
      TimeUnit.SECONDS.sleep(duration);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    String ret = sender + ": " + title;
    return ret;
  }

}

public class Main {
  public static void main(String[] args) {
    ExecutorService executor = (ExecutorService) Executors.newCachedThreadPool();
    CompletionService<String> service = new ExecutorCompletionService<>(executor);

    TaskRequest faceRequest = new TaskRequest("A", service);
    TaskRequest onlineRequest = new TaskRequest("B", service);
    Thread faceThread = new Thread(faceRequest);
    Thread onlineThread = new Thread(onlineRequest);

    TaskProcessor processor = new TaskProcessor(service);
    Thread senderThread = new Thread(processor);

    faceThread.start();
    onlineThread.start();
    senderThread.start();

    try {
      System.out.println("Main: Waiting for the generators.\n");
      faceThread.join();
      onlineThread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Shutdown the executor
    System.out.println("Main: Shuting down the executor.\n");
    executor.shutdown();
    try {
      executor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    // End the execution of the ReportSender
    processor.setEnd(true);
    System.out.println("Main: Ends\n");
  }
}



PreviousNext

Related