/Java-MultiThreading

Java-MultiThreading demonstrates basic concurrency in Java using threads. It shows how to create threads with Runnable, Thread, and lambdas, highlighting interleaved outputs and nondeterministic scheduling.

Primary LanguageJava

Java Multithreading Examples

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.


👨‍💻 Programmer

Anuraag Raj


📂 Project Structure

  • MultiThreadingExamples.java
    Contains three approaches to multithreading:
    1. Implementing the Runnable interface\
    2. Extending the Thread class\
    3. Using anonymous classes / lambda expressions

🚀 How to Run

  1. Compile the program:

    javac MultiThreadingExamples.java
  2. Run the program:

    java MultiThreadingExamples

📝 What You Will See

  • 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 ===

🎯 Learning Outcomes

  • 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.

📖 Notes

  • 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.