fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.concurrent.atomic.AtomicInteger;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class AtomicIntegerTest {
  10. public static void main(String[] args) {
  11. int n = 200_000_000;
  12. int i = 0;
  13. long start = System.currentTimeMillis();
  14. while (i < n) {
  15. i++;
  16. }
  17. long end = System.currentTimeMillis();
  18. System.out.println("int: " + (end - start) + "ms");
  19.  
  20. Integer in = 0;
  21. start = System.currentTimeMillis();
  22. while (in < n) {
  23. in++;
  24. }
  25. end = System.currentTimeMillis();
  26. System.out.println("Integer: " + (end - start) + "ms");
  27.  
  28. AtomicInteger ai = new AtomicInteger(0);
  29. start = System.currentTimeMillis();
  30. while (ai.get() < n) {
  31. ai.incrementAndGet();
  32. }
  33. end = System.currentTimeMillis();
  34. System.out.println("AtomicInteger: " + (end - start) + "ms");
  35. }
  36. }
Success #stdin #stdout 2.74s 125968KB
stdin
Standard input is empty
stdout
int: 3ms
Integer: 582ms
AtomicInteger: 2023ms