CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 2 min read

Exercises

Easy

Easy 1: Name the parts (20 minutes)

For an order system that sends confirmation emails, name the producer, the queue, the consumer and what happens if the mail server is slow.

Done when: four parts are named and the slow case is explained in two sentences.

Hint

The queue sits between the order code and the mail sender.

Easy 2: Run the mail queue (20 minutes)

Type the producer-consumer code with a queue of 2 and 6 jobs. Print the order sent.

Done when: all six print in order and the program ends.

Hint

ArrayBlockingQueue takes the capacity in its constructor.

Easy 3: Squares in a pool (20 minutes)

Run the five squares on a pool of three threads and print the sum.

Done when: it prints 55 and exits.

Hint

invokeAll is an alternative to submitting one by one.

Medium

Medium 1: Two consumers (45 minutes)

Run two consumers on one queue. Stop them with two poison pills. Count jobs handled by each.

Done when: the counts add to the number of jobs and both threads end.

Hint

Put the same pill twice.

Medium 2: Failing task (45 minutes)

Submit five tasks to a pool where one throws. Show how the failure appears and how the others continue.

Done when: the output shows four results and one caught ExecutionException with its cause.

Hint

get() on the failed future throws.

Medium 3: Reject and retry (45 minutes)

Build the one-thread, one-place pool and submit ten tasks. Count accepted and rejected. Then switch to caller-runs and count again.

Done when: abort rejects some, and caller-runs rejects none and slows the submitter.

Hint

new ThreadPoolExecutor.CallerRunsPolicy().

Hard

Hard 1: Notification service (90 minutes)

Build NotificationService with send(user, msg) that queues work and returns at once. A pool sends in the background. Handle shutdown, failure of one send, and a full queue.

Done when: a test sends 100 messages, kills one on purpose, and shuts down with all others delivered.

Hint

Keep the queue bounded and reject when full.

Hard 2: Parallel total (90 minutes)

Split a list of 1,000 numbers into ten parts, sum each in a pool task, and combine the results through futures. Compare with a plain loop.

Done when: both give the same total, and a note says whether the pool helped for this size.

Hint

subList gives the parts.

Hard 3: Choose the pool (90 minutes)

A server receives bursts of 500 requests, each taking 200 ms of waiting on a database. Pick pool type, size, queue size and saturation policy, and justify each with numbers (made up) and a failure case.

Done when: a table lists each setting with a reason, and a paragraph says what the system does at 5,000 requests.

Hint

Waiting tasks suit more threads than cores.