Advertisement
Guest User

ScheduledExecutorServiceTest

a guest
Nov 12th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.concurrent.Executors;
  2. import java.util.concurrent.ScheduledExecutorService;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. public class ScheduledExecutorServiceTest {
  6.  
  7.     // <editor-fold desc="Fields">
  8.    
  9.     private ScheduledExecutorService service;
  10.    
  11.     // <editor-fold>
  12.    
  13.     // <editor-fold desc="Constructor">
  14.    
  15.     public ScheduledExecutorServiceTest() {
  16.         service = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
  17.        
  18.         System.out.println(String.format("Service created with %d processor(s).", Runtime.getRuntime().availableProcessors()));
  19.     }
  20.    
  21.     // <editor-fold>
  22.    
  23.     // <editor-fold desc="Public">
  24.    
  25.     public void run() {
  26.         int max = 10;
  27.         long start = System.currentTimeMillis();
  28.        
  29.         for (int i = 0; i < max; i++) {
  30.             Runnable task = () -> { System.out.println(String.format("%s: %s", Thread.currentThread().getName(), System.currentTimeMillis() - start)); };
  31.            
  32.             service.scheduleWithFixedDelay(task, 0, 300, TimeUnit.MILLISECONDS);
  33.            
  34.             i++;
  35.            
  36.             System.out.println(String.format("Iteration %d of %d.", i, max));
  37.         }      
  38.     }
  39.    
  40.     // </editor-fold>
  41.    
  42.     public static void main(String[] args) {
  43.         ScheduledExecutorServiceTest program = new ScheduledExecutorServiceTest();
  44.        
  45.         program.run();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement