LearnFrenzy
Sign in Take a free test

Multithreading · Exercise 4 of 4

Multithreading Multithreading Programs

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

Java programs that use threads, synchronization and coordination correctly – each written so that its output is deterministic. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Medium

Use two threads – one printing odd numbers and one printing even numbers – coordinated with wait/notify so that 1 to 10 come out in order.

Question page & discussion
Question 2 Medium

Five booking agents (threads) each try to book 30 tickets one at a time from a show with 100 seats; use synchronization so no seat is sold twice and print the totals per agent (sorted) and seats left.

Question page & discussion
Question 3 Medium

Start one thread per document to count its words, store each count in its own array slot, then join and print per-document counts and the grand total.

Question page & discussion
Question 4 Hard

Build a two-stage pipeline: a producer generates order amounts, a processor thread applies 18% GST and passes results through a second bounded buffer to a printer thread; use wait/notify buffers and a poison pill to stop.

Question page & discussion
Question 5 Easy

Run a countdown from 5 in a separate thread (ticking every 20 ms, recording each tick), and in a second run interrupt it after the 2nd tick; print the ticks recorded in each run.

Question page & discussion
Question 6 Hard

Simulate four threads making 5,000 random-looking transfers each among four accounts, using ordered locking to avoid deadlock; verify that the total money is unchanged and no balance is negative.

Question page & discussion
Question 7 Medium

SimpleDateFormat is not thread-safe. Give each of three threads its own instance with ThreadLocal, format the same fixed timestamp in each thread with a different pattern, and print the results after joining.

Question page & discussion
Question 8 Medium

Multiply two 3×3 matrices using one thread per result row; each thread writes only its own row, then main joins all threads and prints the product.

Question page & discussion
Question 9 Hard

A parking lot has 2 slots. Five car threads each park, stay briefly and leave; cars wait when the lot is full. Track the maximum number of cars parked at once and the total served, then print them.

Question page & discussion
Question 10 Medium

Run a background worker that processes 100 items, updating a volatile progress counter; a monitor in main waits (with join) and then reports. Also record which milestones (25%, 50%, 75%, 100%) the worker reached, in order.

Question page & discussion
All exercises doneBack to MultithreadingRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links