flok99

atomic

Jul 29th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3. import java.util.concurrent.atomic.*;
  4. import java.util.concurrent.locks.*;
  5.  
  6. class test implements Runnable {
  7.         AtomicLong al = new AtomicLong(0);
  8.         final long n = 40000000l;
  9.  
  10.         public void run() {
  11.                 for(long i=0; i<n; i++)
  12.                         al.incrementAndGet();
  13.         }
  14.  
  15.         public static void main(String [] args) throws Exception {
  16.                 test inst = new test();
  17.                 inst.run(); // warm-up
  18.  
  19.                 long start = System.nanoTime();
  20.                 inst.run();
  21.                 inst.run();
  22.                 long end1 = System.nanoTime() - start;
  23.                 System.out.println("" + end1 / (double)inst.n);
  24.  
  25.                 Thread t1 = new Thread(inst);
  26.                 Thread t2 = new Thread(inst);
  27.                 start = System.nanoTime();
  28.                 t1.start();
  29.                 t2.start();
  30.                 t1.join();
  31.                 t2.join();
  32.                 long end2 = System.nanoTime() - start;
  33.                 System.out.println("" + end2 / (double)inst.n);
  34.         }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment