Skip to content

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.

Notifications You must be signed in to change notification settings

aryann-7/Java-MultiThreading

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%