Java USES the task architecture to perform a task scheduling example

  • 2020-04-01 02:48:57
  • OfStack


package com.yao;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ExecuteArch {

 
 public static class MyThread implements Runnable {
  public void run() {
   System.out.println("Task repeating. " + System.currentTimeMillis());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    System.out.println("Task interrupted. "
      + System.currentTimeMillis());
   }
  }
 }
 
 public static class MyCallable implements Callable {
  private Future future;
  public MyCallable(Future future) {
   this.future = future;
  }
  public String call() {
   System.out.println("To cancell Task..."
     + +System.currentTimeMillis());
   this.future.cancel(true);
   return "Task cancelled!";
  }
 }
 
 public static void main(String[] args) throws InterruptedException,
   ExecutionException {
  //Produces an ExecutorService object with a thread pool that is resized as needed,
  //When the thread completes the task, it returns to the thread pool for the next task.
  ExecutorService cachedService = Executors.newCachedThreadPool();
  Future myThreadFuture = cachedService.submit(new MyThread());
  Future myCallableFuture = cachedService.submit(new MyCallable(
    myThreadFuture));
  System.out.println(myCallableFuture.get());
  System.out.println("-----------------");
  //Convert the Runnable task to the Callable task
  Callable myThreadCallable = Executors.callable(new MyThread());
  Future myThreadCallableFuture = cachedService.submit(myThreadCallable);
  //For the Runnable task, there is no return value when converted to the Callable task
  System.out.println(myThreadCallableFuture.get());
  cachedService.shutdownNow();
  System.out.println("-----------------");
  //Produces an ExecutorService object with a poolSize thread pool,
  //If the number of tasks is greater than poolSize, the tasks are executed in a queue
  ExecutorService fixedService = Executors.newFixedThreadPool(2);
  fixedService.submit(new MyThread());
  fixedService.submit(new MyThread());
  //Since the thread pool size is 2, subsequent tasks must wait for the previous task to complete before they can be executed.
  myThreadFuture = fixedService.submit(new MyThread());
  myCallableFuture = fixedService.submit(new MyCallable(myThreadFuture));
  System.out.println(myCallableFuture.get());
  fixedService.shutdownNow();
  System.out.println("-----------------");
  //Produces a ScheduledExecutorService object with a thread poolSize of poolSize,
  //If the number of tasks is greater than poolSize, the tasks are in a queue waiting to be executed
  ScheduledExecutorService fixedScheduledService = Executors
    .newScheduledThreadPool(2);
  //New task 1
  MyThread task1 = new MyThread();
  //Use the task execution service to execute task 1 immediately and every 2 seconds thereafter.
  myThreadFuture = fixedScheduledService.scheduleAtFixedRate(task1, 0, 2,
    TimeUnit.SECONDS);
  //New task 2
  MyCallable task2 = new MyCallable(myThreadFuture);
  //Use the task execution service to wait for 5 seconds before executing task 2, which turns task 1 off.
  myCallableFuture = fixedScheduledService.schedule(task2, 5,
    TimeUnit.SECONDS);
  System.out.println(myCallableFuture.get());
  fixedScheduledService.shutdownNow();
 }
}


Related articles: