This project demonstrates different ways of creating and running threads
in Java, using simple Hello and Bye outputs.
It is designed as a reference/tutorial for learning the basics of
concurrent programming.
Anuraag Raj
- MultiThreadingExamples.java
Contains three approaches to multithreading:- Implementing the
Runnableinterface\ - Extending the
Threadclass\ - Using anonymous classes / lambda expressions
- Implementing the
-
Compile the program:
javac MultiThreadingExamples.java
-
Run the program:
java MultiThreadingExamples
- Multiple threads printing
"Hello"and"Bye"messages concurrently. - The order of outputs changes each run, because thread scheduling is nondeterministic.
Sample (varies per run):
=== Example 1: Runnable Interface ===
[Runnable] Hello 1
[Runnable] Bye 1
[Runnable] Hello 2
[Runnable] Bye 2
=== Example 2: Extending Thread Class ===
[Thread] Hello 1
[Thread] Bye 1
...
=== Example 3: Anonymous Runnable ===
[Anonymous] Hello 1
[Anonymous] Bye 1
...
=== All threads completed ===
- Understand how to create and run threads in Java.
- Observe interleaving outputs from concurrent execution.
- Compare different methods (
Runnable,Thread, and lambdas) for creating threads.
- Output order may vary because the JVM and OS decide thread scheduling.\
Thread.sleep()calls are added for clarity, to slow down output and make interleaving observable.