LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Exercise 4 of 4

Concurrency and Executors Concurrency and Executors Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Java programs that use executors, CompletableFuture, atomics, concurrent collections and synchronizers to solve real problems – each with deterministic output. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21.

Question 1 Easy

Simulate generating thumbnails for 8 images on a fixed pool of 4 threads (each takes 50 ms); collect each image's thumbnail size through Futures in order and print the results and total.

Question page & discussion
Question 2 Medium

For an order, fetch the customer name, the product price and the delivery estimate in parallel with CompletableFuture, combine them into an OrderSummary record and print it.

Question page & discussion
Question 3 Medium

Build a pipeline: a producer puts orders into a LinkedBlockingQueue, two workers from an executor validate them and add valid order totals to a LongAdder, using a poison pill per worker to stop; print the totals.

Question page & discussion
Question 4 Medium

An external API allows at most 3 simultaneous calls. Submit 10 calls to a pool of 10 threads, guard them with a Semaphore(3), record the maximum concurrency observed and the number of successful calls.

Question page & discussion
Question 5 Medium

Start four application services (with different start-up times) in parallel; use a CountDownLatch to wait until all are ready (with a timeout), record each service's status in a ConcurrentHashMap, and print a sorted status report.

Question page & discussion
Question 6 Hard

Write a RecursiveTask that counts words in a large array of lines by splitting it in halves until a chunk has at most 1,000 lines; run it on a ForkJoinPool and compare the result with a sequential count.

Question page & discussion
Question 7 Medium

Simulate fetching 2,000 web pages (each "request" sleeps 50 ms) using a virtual-thread-per-task executor; collect each page's status code through Futures and print the counts of 200, 404 and 500 responses.

Question page & discussion
Question 8 Hard

Three worker threads each update their own region of a shared array in steps; after every step all workers wait at a CyclicBarrier whose action records the array total. Run 3 steps and print the recorded totals.

Question page & discussion
Question 9 Medium

Download the same file from three mirrors with different speeds (one fails); use invokeAny to take the first successful result, and print it and how long it took relative to the slowest mirror.

Question page & discussion
Question 10 Hard

Build a price cache guarded by a ReentrantReadWriteLock; run many reader tasks and a few updater tasks on a pool, count reads and writes with atomics, and print the final prices and counts.

Question page & discussion
All exercises doneBack to Concurrency and ExecutorsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links