SHARE
TWEET

stream api, parallel processing

DulcetAirman Jan 27th, 2018 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.concurrent.ConcurrentSkipListSet;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.IntStream;
  8.  
  9. public class SomeClass {
  10.  
  11.     public static void main(String[] args) {
  12.         List<String> list2 = IntStream.range(0, 100000).parallel().mapToObj(i -> process(i))
  13.                 .collect(Collectors.toList());
  14.  
  15.         System.out.println(list2.subList(0, 30));
  16.         System.out.println(THREAD_NAMES);
  17.  
  18.     }
  19.  
  20.     final static Set<String> THREAD_NAMES = new ConcurrentSkipListSet<>();
  21.  
  22.     /**
  23.      * Absurd implementation!
  24.      *
  25.      * @returns "even" or "odd"
  26.      */
  27.     static String process(int i) {
  28.         THREAD_NAMES.add(Thread.currentThread().getName());
  29.         String binary = Integer.toBinaryString(i);
  30.         while (binary.length() > 1)
  31.             binary = binary.substring(1);
  32.         return binary.charAt(binary.length() - 1) == '0' ? "even" : "odd";
  33.     }
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top