Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.util.concurrent.*;
  2.  
  3. class CheckResults {
  4.     private static int counter = 0;
  5.  
  6.     public static void main(String[] args) throws InterruptedException, ExecutionException {
  7.         ExecutorService service = null;
  8.         try {
  9.             service = Executors.newSingleThreadExecutor();
  10.             Future<?> result = service.submit(() -> {
  11.                 for (int i = 0; i < 500000; i++) CheckResults.counter++;
  12.             });
  13.             result.get(1, TimeUnit.NANOSECONDS);
  14.             System.out.println("Reached!");
  15.         } catch (TimeoutException e) {
  16.             e.printStackTrace();
  17.             System.out.println("Not reached in time!");
  18.         } finally {
  19.             if (service != null) service.shutdown();
  20.         }
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement