piffy

Executor 2

Jul 17th, 2021
753
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.concurrent.*;
  2.  
  3. public class Esempio2 {
  4.  
  5.     public static class Counter implements Runnable {
  6.         int n=0;
  7.         @Override
  8.         public void run() {
  9.             System.out.println(n);
  10.             n++;}
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.         Counter r = new Counter() ;
  15.         ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  16.         executorService.scheduleAtFixedRate(r, 5, 1, TimeUnit.SECONDS);
  17.         executorService.execute(r);
  18.         try {
  19.             TimeUnit.SECONDS.sleep(11);
  20.         } catch (InterruptedException e) {
  21.             e.printStackTrace();
  22.         }
  23.         System.out.println("Fine");
  24.         executorService.shutdown(); }
  25. }
  26.  
RAW Paste Data