Skip to content

Latest commit

 

History

History
20 lines (13 loc) · 1.42 KB

File metadata and controls

20 lines (13 loc) · 1.42 KB

Multithreading with Runnable Interface

Course: Object-Oriented Programming in JAVA Lab (BCA39109) | Semester: BCA 3rd Sem

This project demonstrates the creation and execution of multiple threads in Java using the Runnable interface. It effectively illustrates concurrent execution, thread suspension, and exception handling within a multithreaded environment.

🧠 Core OOP & Concurrency Concepts Demonstrated

  • Runnable Interface: The class implements java.lang.Runnable, which is the preferred approach for thread creation as it allows the class to extend another class if needed (unlike extending the Thread class directly).
  • Thread Lifecycle (start vs run): The program explicitly uses t1.start() to spawn a new call stack for concurrent execution, rather than just calling run() which would execute synchronously.
  • Thread Suspension (sleep): Utilizing Thread.sleep(500) to pause the thread execution for exactly 500 milliseconds, demonstrating how the CPU switches context between Thread-1 and Thread-2.
  • Exception Handling: Using a try-catch block to handle InterruptedException, which is required whenever a thread is forced to sleep or wait.

🚀 How to Compile and Run

To run this program, ensure you have the Java Development Kit (JDK) installed on your machine.

  1. Open your terminal or command prompt.
  2. Navigate to the directory containing the file.
  3. Compile the Java code: