Chapter 8
Managing Concurrent Code Execution

  1. How many times does the following code print How rude! at runtime?
    public class Speaking {
       void talk() {
          try {
             Thread.sleep(10_000);
          } catch (InterruptedException e) {
             System.out.println("How rude!");
          } }
       public static void main(String[] args) {
          var s = new Speaking();
          var t = new Thread(() -> s.talk());
          t.run();
          t.interrupt();
          t.interrupt();
          t.interrupt();
       } }
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. The code does not compile.
    6. The code compiles but an exception is printed at runtime.
  2. What is the output of the following code snippet?
    Callable c = new Callable() {
       public Object run() {
          System.out.print("X");
          return 10;
       }
    };
    var s = Executors.newScheduledThreadPool(1);
    for(int i=0; i<10; i++) {
       Future f = s.submit(c);
       f.get();
    }
    s.shutdown();
    System.out.print("Done!");
     
    1. XXXXXXXXXXDone!
    2. Done!XXXXXXXXXX
    3. The code does not compile.
    4. The code hangs indefinitely at runtime.
    5. The code throws an exception at runtime.
    6. The output cannot be determined ahead of time.
  3. Which of the following methods is not available on an ExecutorService instance? (Choose two.)
    1. execute(Callable)
    2. shutdownNow()
    3. submit(Runnable)
    4. exit()
    5. submit(Callable)
    6. execute(Runnable)
  4. The following program simulates flipping a coin an even number of times. Assuming five seconds is enough time for all of the tasks to finish, what is the output of the following application?
    import java.util.concurrent.*;
    import java.util.concurrent.atomic.*;
    public class Luck {
       private AtomicBoolean coin = new AtomicBoolean(false);
       void flip() {
          coin.getAndSet(!coin.get());
       }
       public static void main(String[] gamble) throws Exception {
          var luck = new Luck();
          ExecutorService s = Executors.newCachedThreadPool();
          for(int i=0; i<1000; i++) {
             s.execute(() -> luck.flip());
          }
          s.shutdown();
          Thread.sleep(5000);
          System.out.println(luck.coin.get());
       } }
     
    1. false
    2. true
    3. The code does not compile.
    4. The code hangs indefinitely at runtime.
    5. The code throws an exception at runtime.
    6. The output cannot be determined ahead of time.
  5. Which of the following is a recommended way to define an asynchronous task?
    1. Create a Callable expression and pass it to an instance of an Executor.
    2. Create a class that extends Thread and override the start() method.
    3. Create a Runnable lambda expression and pass it to a Thread constructor.
    4. Create an anonymous Runnable class that overrides the begin() method.
    5. All of the above.
  6. Assuming five seconds is enough time for the threads in this program to complete, what is the expected result of executing the following program?
    import java.util.*;
    public class Dance {
       int count = 0;
       synchronized int step() { return count++; }
       public static void main(String[] args) throws InterruptedException {
          var waltz = new Dance();
          var dancers = new ArrayList<Thread>();
     
          for(int i=0; i<10; i++)
             dancers.add(new Thread(() -> waltz.step()));
          for(Thread dancer : dancers)
             dancer.start();      
          dancers.forEach(d -> d.interrupt());
     
          Thread.sleep(5_000);
          System.out.print(waltz.count);
       } }
     
    1. It always prints a number less than 10.
    2. It always prints 10.
    3. It may print 10 or a number less than 10.
    4. The code does not compile.
    5. The code compiles but an exception is printed at runtime.
    6. None of the above.
  7. Given the following program, how many times is Locked! expected to be printed?
    import java.util.concurrent.locks.*;
    public class Padlock {
       private Lock lock = new ReentrantLock();
       public void lockUp() {
          if (lock.tryLock()) {
            lock.lock();
             System.out.println("Locked!");
             lock.unlock();
          }
       }
       public static void main(String… unused) throws Exception {
          var gate = new Padlock();
          for(int i=0; i<5; i++) {
             new Thread(() -> gate.lockUp()).start();
             Thread.sleep(1_000);
          } } }
     
    1. One time.
    2. Five times.
    3. The code does not compile.
    4. The code hangs indefinitely at runtime.
    5. The code throws an exception at runtime.
    6. The output cannot be determined ahead of time.
  8. Which statements about the following application are correct? (Choose two.)
    import java.util.concurrent.atomic.*;
    import java.util.stream.*;
    public class TicketTaker {
       long ticketsSold;
       final AtomicInteger ticketsTaken;
       public TicketTaker() {
          ticketsSold = 0;
          ticketsTaken = new AtomicInteger(0);
       }
       public void performJob() {
          IntStream.iterate(1, p -> p+1)
             .parallel()
             .limit(100)
             .forEach(i -> ticketsTaken.getAndIncrement());
          IntStream.iterate(1, q -> q+1)
             .parallel()
             .limit(500)
             .forEach(i -> ++ticketsSold);
          System.out.print(ticketsTaken+" "+ticketsSold);
       }
       public static void main(String[] matinee) {
          new TicketTaker().performJob();
       } }
     
    1. The TicketTaker constructor does not compile.
    2. The performJob() method does not compile.
    3. The class compiles.
    4. The first number printed is consistently 100.
    5. The second number printed is consistently 500.
    6. A ConcurrentModificationException is thrown at runtime.
  9. Given the original array, how many of the following for statements result in an exception at runtime, assuming each is executed independently?
    var original = List.of(1,2,3,4,5);
     
    var copy1 = new CopyOnWriteArrayList<Integer>(original);
    for(Integer w : copy1)
       copy1.remove(w);
     
    var copy2 = Collections.synchronizedList(original);
    for(Integer w : copy2)
       copy2.remove(w);
     
    var copy3 = new ArrayList<Integer>(original);
    for(Integer w : copy3)
       copy3.remove(w);
     
    var copy4 = new ConcurrentLinkedQueue<Integer>(original);
    for(Integer w : copy4)
       copy4.remove(w);
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. The code does not compile.
  10. Fill in the blanks: ______________ is a special case of ______________, in which two or more active threads try to acquire the same set of locks and are repeatedly unsuccessful.
    1. Deadlock, livelock
    2. Deadlock, resource starvation
    3. Livelock, resource starvation
    4. Resource starvation, race conditions
    5. Resource starvation, livelock
    6. None of the above
  11. What is the output of the following program?
    import java.util.stream.*;
    public class Bull {
       void charge() {
          IntStream.range(1,6)
             .parallel()
             .forEachOrdered(System.out::print);
       }
       public static void main(String[] args) {
          var b = new Bull();
          b.charge();
       } }
     
    1. 12345
    2. 54321
    3. The output cannot be determined ahead of time.
    4. The code does not compile.
    5. An exception is thrown at runtime.
    6. None of the above.
  12. What is the output of the following application?
     3:  public class TpsReport {
     4:     public void submitReports() {
     5:        var s = Executors.newCachedThreadPool();
     6:        Future bosses = s.submit(() -> System.out.print("1"));
     7:        s.shutdown();
     8:        System.out.print(bosses.get());
     9:     }
     10:    public static void main(String[] memo) {
     11:       new TpsReport().submitReports();
     12:    } }
     
    1. null
    2. 1null
    3. 1
    4. Line 6 does not compile.
    5. Line 8 does not compile.
    6. An exception is thrown at runtime.
  13. Which of the following static methods do not exist in the Executors class? (Choose two.)
    1. newFixedScheduledThreadPool()
    2. newThreadPool()
    3. newFixedThreadPool(int)
    4. newSingleThreadExecutor()
    5. newScheduledThreadPool(int)
    6. newSingleThreadScheduledExecutor()
  14. How many times does the following application print Ready at runtime?
    package parade;
    import java.util.concurrent.*;
    public class CartoonCat {
       private void await(CyclicBarrier c) {
          try {
             c.await();
          } catch (Exception e) {}
       }
       public void march(CyclicBarrier c) {
          var s = Executors.newSingleThreadExecutor();
          for(int i=0; i<12; i++)
             s.execute(() -> await(c));
          s.shutdown();
       }
       public static void main(String… strings) {
          new CartoonCat().march(new CyclicBarrier(4,
                () -> System.out.println("Ready")));
       } }
     
    1. Zero
    2. One
    3. Three
    4. The code does not compile.
    5. An exception is thrown at runtime.
  15. Let's say you needed a thread executor to create tasks for a CyclicBarrier that has a barrier limit of five threads. Which static method in ExecutorService should you use to obtain it?
    1. newSingleThreadExecutor()
    2. newSingleThreadScheduledExecutor()
    3. newCachedThreadPool()
    4. newFixedThreadPool(2)
    5. None of the above
  16. The following diagrams represent the order of read/write operations of two threads sharing a common variable. Each thread first reads the value of the variable from memory and then writes a new value of the variable back to memory. Which diagram demonstrates proper synchronization?
    1. A flow diagram of the order of read/write operations of two threads sharing a common variable.
    2. A flow diagram of the order of read/write operations of two threads sharing a common variable.

    3. A flow diagram of the order of read/write operations of two threads sharing a common variable.
    4. A flow diagram of the order of read/write operations of two threads sharing a common variable.
  17. What is the output of the following program? (Choose three.)
     import java.util.concurrent.atomic.*;
     import java.util.stream.*;
     public class Circus {
        private static int seal = 0;
        private static volatile int tiger = 0;
        private static AtomicInteger lion = new AtomicInteger(0);
        public static void main(String[] tent) {
           Stream.iterate(0, x -> x + 1)
              .parallel()
              .limit(100)
              .forEach(q -> {seal++; tiger++; lion.incrementAndGet();});
           System.out.println(seal + "," + tiger + ","+ lion);
        } }
     
    1. The first number printed may be less than 100.
    2. The first number printed is always 100.
    3. The second number printed may be less than 100.
    4. The second number printed is always 100.
    5. The third number printed may be less than 100.
    6. The third number printed is always 100.
  18. What is the output of the following application?
     import java.util.*;
     import java.util.concurrent.*;
     public class Race {
        ExecutorService service = Executors.newFixedThreadPool(8);
        public static int sleep() {
           try { Thread.sleep(1000); } catch (Exception e) {}
           return 1;
        }
        public void hare() {
           try {
              Callable<Integer> c = () -> sleep();
              final var r = List.of(c,c,c);
              var results = service.invokeAll(r);
              System.out.println("Hare won the race!");
           } catch (Exception e) {e.printStackTrace();}
        }
        public void tortoise() {
           try {
              Callable<Integer> c = () -> sleep();
              final var r = List.of(c,c,c);
              Integer result = service.invokeAny(r);
              System.out.println("Tortoise won the race!");
           } catch (Exception e) {e.printStackTrace();}
        }
        public static void main(String[] p) throws Exception {
           var race = new Race();
           race.service.execute(() -> race.hare());
           race.service.execute(() -> race.tortoise());
        } }
     
    1. Hare won the race! is printed first.
    2. Tortoise won the race! is printed first.
    3. The code does not compile.
    4. The code hangs indefinitely at runtime.
    5. The code throws an exception at runtime.
    6. The output cannot be determined ahead of time.
  19. Which of the following concurrent collections is sorted? (Choose two.)
    1. ConcurrentSkipList
    2. ConcurrentSkipListSet
    3. CopyOnWriteArrayList
    4. ConcurrentSkipListMap
    5. ConcurrentLinkedQueue
    6. LinkedBlockingQueue
  20. What is the output of the following application?
     package taxes;
     import java.util.concurrent.*;
     public class Accountant {
        public static void completePaperwork() {
           System.out.print("[Filing]");
        }
        public static double getPi() {
           return 3.14159;
        }
        public static void main(String[] args) throws Exception {
           ExecutorService x = Executors.newSingleThreadExecutor();
           Future<?> f1 = x.submit(() -> completePaperwork());
           Future<Object> f2 = x.submit(() -> getPi());
           System.out.print(f1.get()+" "+f2.get());
           x.shutdown();
        } }
     
    1. [Filing]
    2. [Filing]3.14159
    3. [Filing]null 3.14159
    4. The declaration of f1 does not compile.
    5. The declaration of f2 does not compile.
    6. The output cannot be determined ahead of time.
  21. Assuming 10 seconds is enough time for all the tasks to finish, what statements about the following program are correct? (Choose two.)
     import java.util.concurrent.*;
     import java.util.concurrent.atomic.*;
     public class Clock {
        private AtomicLong bigHand = new AtomicLong(0);
        void incrementBy10() {
           bigHand.getAndSet(bigHand.get() + 10);
        }
        public static void main(String[] c) throws Exception {
           var smartWatch = new Clock();
           ExecutorService s = Executors.newCachedThreadPool();
           for(int i=0; i<100; i++) {
              s.submit(() -> smartWatch.incrementBy10()).get();
           }
           s.shutdown();
           s.awaitTermination(10, TimeUnit.SECONDS);
           System.out.println(smartWatch.bigHand.get());
        } }
     
    1. The code does not compile.
    2. The incrementBy10() method is thread-safe.
    3. The incrementBy10() method is not thread-safe.
    4. The output is 1000 on every execution.
    5. The output cannot be determined ahead of time.
    6. The code hangs indefinitely at runtime.
  22. What is the most likely result of executing the following application?
     package jokes;
     import java.util.concurrent.*;
     public class Riddle {
        public void sleep() {
           try { Thread.sleep(5000); } catch (Exception e) {}
        }
        public String getQuestion(Riddle r) {
           synchronized {
              sleep();
              if(r != null) r.getAnswer(null);
              return "How many programmers does it take "
                    + "to change a light bulb?";
           } }
        public synchronized String getAnswer(Riddle r) {
           sleep();
           if(r != null) r.getAnswer(null);
           return "None, that's a hardware problem";
        }
      
        public static void main(String… ununused) {
           var r1 = new Riddle();
           var r2 = new Riddle();
           var s = Executors.newFixedThreadPool(2);
           s.submit(() -> r1.getQuestion(r2));
           s.execute(() -> r2.getAnswer(r1));
           s.shutdown();
        } }
     
    1. A deadlock is produced at runtime.
    2. A livelock is produced at runtime.
    3. The application completes successfully.
    4. The code does not compile.
    5. The code hangs indefinitely at runtime.
    6. The output cannot be determined ahead of time.
  23. Which ScheduledExecutorService method can result in the same action being executed by two threads at the same time?
    1. scheduleAtFixedDelay()
    2. scheduleAtFixedRate()
    3. scheduleWithFixedDelay()
    4. scheduleAtSameRate()
    5. scheduleWithRate()
    6. None of the above
  24. What is the output of the following application?
     package olympics;
     import java.util.concurrent.*;
     public class Athlete {
        int stroke = 0;
        public synchronized void swimming() {
           stroke++;
        }
        private int getStroke() {
           synchronized(this) { return stroke; }
        }
        public static void main(String… laps) {
           ExecutorService s = Executors.newFixedThreadPool(10);
           Athlete a = new Athlete();
           for(int i=0; i<1000; i++) {
              s.execute(() -> a.swimming());
           }
           s.shutdown();
           System.out.print(a.getStroke());
        } }
     
    1. A deadlock is produced at runtime.
    2. A livelock is produced at runtime.
    3. 1000
    4. The code does not compile.
    5. The result is unknown until runtime because stroke is not written in a thread-safe manner and a write may be lost.
    6. None of the above.
  25. Which of the following is most likely to be caused by a race condition?
    1. A thread perpetually denied access to a resource
    2. A program hanging indefinitely
    3. An int variable incorrectly reporting the number of times an operation was performed
    4. Two threads actively trying to restart a blocked process that is guaranteed to always end the same way
    5. Two threads endlessly waiting on each other to release shared locks
  26. Which statement about the following class is correct?
     package my;
     import java.util.*;
     public class ThreadSafeList {
        private List<Integer> data = new ArrayList<>();
        public synchronized void addValue(int value) {
           data.add(value);
        }
        public int getValue(int index) {
           return data.get(index);
        }
        public int size() {
           synchronized(ThreadSafeList.class) {
              return data.size();
           } } }
     
    1. The code compiles and is thread-safe.
    2. The code compiles and is not thread-safe.
    3. The code does not compile because of the size() method.
    4. The code does not compile because of the getValue() method.
    5. The code does not compile for another reason.
    6. None of the above.
  27. Which two method names, when filled into the print2() method, produce the same output as the print1() method? Assume the input arguments for each represent the same non-null numeric value.
     public static synchronized void print1(int counter) {
        System.out.println(counter--);
        System.out.println(++counter);
     }
      
     public static synchronized void print2(AtomicInteger counter) {
        System.out.println(counter._________________);
        System.out.println(counter._________________);
     }
     
    1. decrementAndGet() and getAndIncrement()
    2. decrementAndGet() and incrementAndGet()
    3. getAndDecrement() and getAndIncrement()
    4. getAndDecrement() and incrementAndGet()
    5. None of the above
  28. What is the result of executing the following application multiple times?
     package bears;
     import java.util.*;
     public class Bounce {
        public static void main(String… legend) {
           List.of(1,2,3,4).stream()
              .forEach(System.out::println);
           List.of(1,2,3,4).parallel()
              .forEach(System.out::println);
           List.of(1,2,3,4).parallel()
              .forEachOrdered(System.out::println);
        } }
     
    1. Only the first stream prints the same order every time.
    2. Only the first and second streams print the same order every time.
    3. Only the first and third streams print the same order every time.
    4. All of the streams print the same order every time.
    5. None of the streams prints the same order every time.
    6. None of the above.
  29. How many lines of the following code snippet contain compilation errors?
     11: ScheduledExecutorService t = Executors
     12:    .newSingleThreadScheduledExecutor();
     13: Future result = t.execute(System.out::println);
     14: t.invokeAll(null);
     15: t.scheduleAtFixedRate(() -> {return;},5,TimeUnit.MINUTES);
     
    1. None
    2. One
    3. Two
    4. Three
    5. None of the above
  30. How many times does the following application print W at runtime?
     package crew;
     import java.util.concurrent.*;
     import java.util.stream.*;
     public class Boat {
        private void waitTillFinished(CyclicBarrier c) {
           try {
              c.await();
              System.out.print("W");
           } catch (Exception e) {}
        }
        public void row(ExecutorService s) {
           var cb = new CyclicBarrier(5);
           IntStream.iterate(1, i-> i+1)
              .limit(12)
              .forEach(i -> s.submit(() -> waitTillFinished(cb)));
        }
        public static void main(String[] oars) {
           ExecutorService service = null;
           try {
              service = Executors.newCachedThreadPool();
              new Boat().row(service);
           } finally {
              service.isShutdown();
           } } }
     
    1. Zero.
    2. Ten.
    3. Twelve.
    4. The code does not compile.
    5. The output cannot be determined ahead of time.
    6. None of the above.
  31. Using the Boat class from the previous question, what is the final state of the application?
    1. The application produces an exception at runtime.
    2. The application terminates successfully.
    3. The application hangs indefinitely because the ExecutorService is never shut down.
    4. The application hangs at runtime.
    5. None of the above.
  32. Given the following code snippet, what statement about the values printed on lines p1 and p2 is correct?
     var db = Collections.synchronizedList(new ArrayList<>());
     IntStream.range(1,6)
        .parallel()
        .map(i -> {db.add(i); return i;})
        .forEachOrdered(System.out::print);  // p1
     System.out.println();
     db.forEach(System.out::print);          // p2
     
    1. They are always the same.
    2. They are sometimes the same.
    3. They are never the same.
    4. The code does not compile.
    5. The code will produce a ConcurrentModificationException at runtime.
    6. None of the above.
  33. Given the following program, how many times is TV Time expected to be printed? Assume 10 seconds is enough time for each task created by the program to complete.
     import java.util.concurrent.*;
     import java.util.concurrent.locks.*;
     public class Television {
        private static Lock myTurn = new ReentrantLock();
        public void watch() {
           try {
              if (myTurn.lock(5, TimeUnit.SECONDS)) {
                 System.out.println("TV Time");
                 myTurn.unlock();
              }
           } catch (InterruptedException e) {}
        }
        public static void main(String[] t) throws Exception {
           var newTv = new Television();
           for (int i = 0; i < 3; i++) {
              new Thread(() -> newTv.watch()).start();
              Thread.sleep(10*1000);
           }
        } }
     
    1. One time.
    2. Three times.
    3. The code does not compile.
    4. The code hangs indefinitely at runtime.
    5. The code throws an exception at runtime.
    6. The output cannot be determined ahead of time.
  34. Given the original array, how many of the following for statements enter an infinite loop at runtime, assuming each is executed independently?
     var original = new ArrayList<Integer>(List.of(1,2,3));
      
     var copy1 = new ArrayList<Integer>(original);
     for(Integer q : copy1)
        copy1.add(1);
      
     var copy2 = new CopyOnWriteArrayList<Integer>(original);
     for(Integer q : copy2)
        copy2.add(2);
      
     var copy3 = new LinkedBlockingQueue<Integer>(original);
     for(Integer q : copy3)
        copy3.offer(3);
      
     var copy4 = Collections.synchronizedList(original);
     for(Integer q : copy4)
        copy4.add(4);
     
    1. Zero
    2. One
    3. Two
    4. Three
    5. Four
    6. The code does not compile.
  35. Which ExecutorService method guarantees all running tasks are stopped in an orderly fashion?
    1. shutdown()
    2. shutdownNow()
    3. halt()
    4. shutdownAndTerminate()
    5. None of the above
  36. Assuming 10 seconds is enough time for all of the tasks to finish, what is the output of the following application?
     import java.util.concurrent.*;
     public class Bank {
        static int cookies = 0;
        public synchronized void deposit(int amount) {
           cookies += amount;
        }
        public static synchronized void withdrawal(int amount) {
           cookies -= amount;
        }
        public static void main(String[] amount) throws Exception {
           var teller = Executors.newScheduledThreadPool(50);
           Bank bank = new Bank();
           for(int i=0; i<25; i++) {
              teller.submit(() -> bank.deposit(5));
              teller.submit(() -> bank.withdrawal(5));
           }
           teller.shutdown();
           teller.awaitTermination(10, TimeUnit.SECONDS);
            System.out.print(bank.cookies);
        } }
     
    1. 0
    2. 125
    3. -125
    4. The code does not compile.
    5. The result is unknown until runtime.
    6. An exception is thrown.
  37. What is the output of the following application?
     import java.util.*;
     public class SearchList<T> {
        private List<T> data;
        private boolean foundMatch = false;
        public SearchList(List<T> list) {
           this.data = list;
        }
        public void exists(T v,int start, int end) {
           if(end-start==0) {}
           else if(end-start==1) {
              foundMatch = foundMatch || v.equals(data.get(start));
           } else {
              final int middle = start + (end-start)/2;
              new Thread(() -> exists(v,start,middle)).run();
              new Thread(() -> exists(v,middle,end)).run();
           }
        }
        public static void main(String[] a) throws Exception {
           List<Integer> data = List.of(1,2,3,4,5,6);
           SearchList<Integer> t = new SearchList<Integer>(data);
           t.exists(5, 0, data.size());
           System.out.print(t.foundMatch);
        } }
     
    1. true
    2. false
    3. The code does not compile.
    4. The result is unknown until runtime.
    5. An exception is thrown.
    6. None of the above.
  38. Given the following code snippet, what statement about the values printed on lines q1 and q2 is correct?
     var mitchsWorkout = new CopyOnWriteArrayList<Integer>();
     List.of(1,5,7,9).stream().parallel()
        .forEach(mitchsWorkout::add);
     mitchsWorkout
        .forEachOrdered(System.out::print);  // q1
     List.of(1,5,7,9).stream().parallel()
        .forEachOrdered(System.out::print);  // q2
     
    1. They are always the same.
    2. They are sometimes the same.
    3. They are never the same.
    4. The code does not compile.
    5. The code will produce a ConcurrentModificationException at runtime.
    6. None of the above.
  39. What is the output of the following program? (Choose three.)
     import java.util.concurrent.atomic.*;
     import java.util.stream.*;
     public class Moon {
        private volatile AtomicLong green = new AtomicLong(0);
        private volatile int cheese = 0;
        private volatile int rocket = 0;
        private void takeOff() {
           Stream.iterate(0, x -> x + 1).parallel().limit(1000).forEach(q -> {
              green.getAndIncrement();
              cheese++;
              synchronized (this) {
                 ++rocket;
              }
           });
           System.out.println(green + "," + cheese + "," + rocket);
        }
        public static void main(String[] tent) {
           new Moon().takeOff();
        } }
     
    1. The first number printed may be less than 1000.
    2. The first number printed is always 1000.
    3. The second number printed may be less than 1000.
    4. The second number printed is always 1000.
    5. The third number printed may be less than 1000.
    6. The third number printed is always 1000.
  40. Assuming the two stream operators are compiled and executed independently, which statements are correct? (Choose two.)
     var drink = List.of("coke", "soda", "pop");
      
     System.out.print(drink.parallelStream()
        .parallel()
        .reduce(0, (c1, c2) -> c1 + c2.length(), (s1, s2) -> s1 + s2));
      
     System.out.print(drink.stream()
        .reduce(0, (c1, c2) -> c1 + c2.length(), (s1, s2) -> s1 + s2));
     
    1. The first number printed is sometimes 11.
    2. The first number printed is always 11.
    3. The second number printed is sometimes 11.
    4. The second number printed is always 11.
    5. The first stream operation does not compile.
    6. The second stream operation does not compile.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.17.64