Java - Multithreading - Hands-on
Learning Objectives
- Understand how to create and manage threads in Java.
- Implement different thread states and synchronization techniques.
- Apply multithreading to real-world scenarios.
Exercise 1: Creating and Starting a Thread
Problem Statement
Write a program that creates and starts a thread by extending the Thread class.
Instructions:
- Define a class that extends
Thread. - Override the
run()method to print a message. - Create an instance of the class and start the thread using
start().
Expected Output:
- The thread should print a message indicating it is running.
Exercise 2: Implementing Runnable Interface
Problem Statement
Create and start a thread using the Runnable interface.
Instructions:
- Define a class that implements
Runnable. - Implement the
run()method to display a message. - Create a
Threadobject and pass the class instance to it. - Start the thread.
Expected Output:
- The thread should print a message indicating it is running.
Exercise 3: Thread Synchronization
Problem Statement
Demonstrate thread synchronization using the synchronized keyword.
Instructions:
- Create a class with a synchronized method.
- Start multiple threads accessing the method.
- Observe how synchronization prevents race conditions.
Expected Output:
- A well-ordered sequence of method execution without interference.
Exercise 4: Thread Sleep and Join
Problem Statement
Use Thread.sleep() and join() to control thread execution timing.
Instructions:
- Create a thread that prints messages with delays using
sleep(). - Start multiple threads and use
join()to wait for one thread to finish before others continue.
Expected Output:
- Delayed messages printed sequentially.
Exercise 5: Thread States
Problem Statement
Simulate different thread states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED.
Instructions:
- Create a thread and print its state at various execution points.
- Use
sleep(),wait(), andjoin()to transition through different states.
Expected Output:
- Console output showing the thread transitioning through different states.
Exercise 6: Copy Files Using Threads
Problem Statement
Write a program that copies files from one directory to another using multiple threads.
Instructions:
- Accept source and destination directory paths as input.
- List all files in the source directory.
- Create multiple threads to copy files concurrently.
Expected Output:
- Files copied efficiently using multiple threads.
🔗 Related Topics: