Imagine you are working on a multi-threaded application where you need to process a list of orders and then store the results in a map. Explain how you can achieve concurrency while using the Stream API.

  • a. Use the parallelStream() method to process orders concurrently and collect results using Collectors.toConcurrentMap().
  • b. Create multiple threads manually and divide the work among them, then merge the results into a concurrent map.
  • c. Use a single thread to process orders and update a synchronized map for concurrent access.
  • d. Use the stream() method and a synchronized block to process orders concurrently and store results in a concurrent map.
Option 'a' is the correct approach. It uses parallelStream() to process orders concurrently and safely stores results in a concurrent map. Option 'b' is feasible but involves more complex threading management. Option 'c' uses a single thread, which doesn't achieve concurrency. Option 'd' attempts concurrency but doesn't utilize the Stream API correctly.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *