fork(1) 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.*;
  7. import java.util.concurrent.atomic.*;
  8. import java.util.stream.*;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone {
  12. public static void main (String[] args) throws java.lang.Exception {
  13. AtomicInteger total = new AtomicInteger();
  14. LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>();
  15. Thread thread = new Thread(() -> {
  16. int remaining = 10000;
  17. while (remaining != 0) {
  18. total.incrementAndGet();
  19. if (q.poll() != null) {
  20. remaining--;
  21. }
  22. }
  23. });
  24. Integer[] first100 = new Integer[100];
  25. for (int i = 0 ; i != 100 ; i++) {
  26. first100[i] = i;
  27. }
  28. long start = System.nanoTime();
  29. thread.start();
  30. for (int i = 0 ; i != 10000 ; i++) {
  31. q.put(first100[i%100]);
  32. }
  33. thread.join();
  34. long runtime = System.nanoTime() - start;
  35. System.out.println(runtime);
  36. System.out.println(total.get());
  37. }
  38. }
Success #stdin #stdout 0.18s 34008KB
stdin
Standard input is empty
stdout
23464563
103953