w3resource

Java Multithreading: Exercises, Practices, Solutions


This resource offers a total of 75 Java Multithreading problems for practice. It includes 15 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]

1. Increment shared counter with multiple threads

Write a Java program to create and start multiple threads that increment a shared counter variable concurrently.

Click me to see the solution

2. Producer-consumer using wait() and notify()

Write a Java program to create a producer-consumer scenario using the wait() and notify() methods for thread synchronization.

Click me to see the solution

3. Synchronize threads using ReentrantLock

Write a Java program that uses the ReentrantLock class to synchronize access to a shared resource among multiple threads.

Click me to see the solution

4. Use Semaphore for thread synchronization

Write a Java program to demonstrate Semaphore usage for thread synchronization.

Click me to see the solution

5. Synchronize threads using CyclicBarrier

Write a Java program to showcase the usage of the CyclicBarrier class for thread synchronization.

Click me to see the solution

6. Coordinate threads with CountDownLatch

Write a Java program that uses the CountDownLatch class to synchronize the start and finish of multiple threads.

Click me to see the solution

7. Use ReadWriteLock for concurrent access

Write a Java program to illustrate the usage of the ReadWriteLock interface for concurrent read-write access to a shared resource.

Click me to see the solution

8. Access map concurrently with ConcurrentHashMap

Write a Java program demonstrating how to access a map concurrently using the ConcurrentHashMap class.

Click me to see the solution

9. Thread-safe queue using ConcurrentLinkedQueue

Write a Java program that utilizes the ConcurrentLinkedQueue class to implement a thread-safe queue.

Click me to see the solution

10. Coordinate threads using Phaser class

Write a Java program to showcase the usage of the Phaser class for coordination and synchronization of multiple threads.

Click me to see the solution

11. Exchange data between threads using Exchanger

Write a Java program that utilizes the Exchanger class for exchanging data between two threads.

Click me to see the solution

12. Use Callable and Future for async execution

Write a Java program to demonstrate the usage of the Callable and Future interfaces for executing tasks asynchronously and obtaining their results.

Click me to see the solution

13. Schedule tasks using ScheduledExecutorService

Write a Java program that uses the ScheduledExecutorService interface to schedule tasks for execution at a specified time or with a fixed delay.

Click me to see the solution

14. Parallel recursion with ForkJoinPool

Write a Java program to demonstrate the usage of the ForkJoinPool class for parallel execution of recursive tasks.

Click me to see the solution

15. Optimize access with StampedLock

Write a Java program that utilizes the StampedLock class for optimizing concurrent read-write access to a shared resource.

Click me to see the solution

Java Code Editor

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.