0% found this document useful (0 votes)
7 views

Basic Multithreading Program 1

This document provides a step-by-step guide for creating a multithreading program in Java using Eclipse. It covers creating a Java project, defining a thread class, and running multiple threads simultaneously, while also explaining the differences between extending the Thread class and implementing the Runnable interface. Additionally, it highlights the expected output and the significance of multithreading in improving program performance.

Uploaded by

Muktar Fakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Basic Multithreading Program 1

This document provides a step-by-step guide for creating a multithreading program in Java using Eclipse. It covers creating a Java project, defining a thread class, and running multiple threads simultaneously, while also explaining the differences between extending the Thread class and implementing the Runnable interface. Additionally, it highlights the expected output and the significance of multithreading in improving program performance.

Uploaded by

Muktar Fakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Step 1: Open Eclipse and Create a New Java Project

1. Open Eclipse IDE.

2. Click on File → New → Java Project.

3. In the Project Name field, enter

MultiThreadingDemo

Click Finish.

Step 2: Create a New Package

1. Inside your new project (MultiThreadingDemo), right-click on src.

2. Select New → Package.

3. Enter a package name, e.g

com.example.threads

Click Finish.

Step 3: Create a Java Class for Multithreading

1. Right-click on the package (com.example.threads).

2. Click New → Class.

3. In the Class Name field, enter:

MyThread

4. Uncheck "public static void main(String[] args)".

5. Click Finish.

Step 4: Write Code for Thread Execution

Method 1: Extending Thread Class

Inside MyThread.java, write this code:


package com.example.threads;

class MyThread extends Thread {

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + " - Count: " + i);

try {

Thread.sleep(1000); // Pause for 1 second

} catch (InterruptedException e) {

e.printStackTrace();

Step 5: Create a Main Class

1. Right-click on the package (com.example.threads).

2. Click New → Class.

3. Enter the class name:

ThreadDemo

4. Check "public static void main(String[] args)".

5. Click Finish.

Step 6: Write the Code to Start Threads

Inside ThreadDemo.java, write this code:

package com.example.threads;

public class ThreadDemo {

public static void main(String[] args) {

MyThread t1 = new MyThread();


MyThread t2 = new MyThread();

t1.setName("Thread 1");

t2.setName("Thread 2");

t1.start(); // Start the first thread

t2.start(); // Start the second thread

Step 7: Run the Program

1. In Eclipse, right-click on ThreadDemo.java.

2. Select Run As → Java Application.

3. You will see output similar to this:

Thread 1 - Count: 1

Thread 2 - Count: 1

Thread 1 - Count: 2

Thread 2 - Count: 2

Thread 1 - Count: 3

Thread 2 - Count: 3

...

Understanding How It Works

 Two threads (t1 and t2) run independently and print numbers from 1 to 5.

 Each thread waits (Thread.sleep(1000)) for 1 second between prints.

 Threads execute in parallel, so the order is not fixed


What is Multithreading?

Multithreading is a feature in Java that allows multiple parts of a program (threads) to run
simultaneously. This improves performance and efficiency.

For example:

 A video player can play video and receive user input at the same time.

 A web browser can load pages and allow scrolling at the same time.

Basic Concept of Threads

A thread is like a separate worker running a task in a program. In Java, you can create threads using
two methods:

1. Extending the Thread class (Directly creating a thread)

2. Implementing the Runnable interface (Recommended way for flexibility)

1️⃣ Creating a Thread by Extending the Thread Class

Let's create a simple multithreading program where two threads run simultaneously and print
numbers.

Step 1: Define a Thread Class

We create a class that extends Thread and overrides the run() method.

package com.example.threads; // This is the package name

// Define a custom thread class by extending Thread

class MyThread extends Thread {

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + " - Count: " + i);

try {

Thread.sleep(1000); // Pause for 1 second (1000 milliseconds)

} catch (InterruptedException e) {

e.printStackTrace();

}
}

What Happens Here?

 run() method: Defines the task the thread will execute.

 Thread.sleep(1000): Makes the thread pause for 1 second to simulate processing time.

2️ Running Threads in main() Method

We create two threads inside the main method and start them.

package com.example.threads; // Package name

public class ThreadDemo {

public static void main(String[] args) {

MyThread t1 = new MyThread(); // Create first thread

MyThread t2 = new MyThread(); // Create second thread

t1.setName("Thread 1");

t2.setName("Thread 2");

t1.start(); // Start first thread

t2.start(); // Start second thread

3️ Explanation of start() vs run()

Wrong way: Calling run() directly → Runs like a normal method, NOT a thread.

t1.run(); // This runs like a normal method, NOT a thread!

t2.run(); // Not multithreading

Correct way: Calling start() → Runs as a separate thread.

t1.start(); // Runs in parallel as a new thread

t2.start(); // Runs in parallel as a new thread


4️ Expected Output

Since the threads run independently, the output may vary each time:

Thread 1 - Count: 1

Thread 2 - Count: 1

Thread 1 - Count: 2

Thread 2 - Count: 2

Thread 1 - Count: 3

Thread 2 - Count: 3

Thread 1 - Count: 4

Thread 2 - Count: 4

Thread 1 - Count: 5

Thread 2 - Count: 5

Here, both threads print numbers in parallel.

5️⃣ Creating Threads Using Runnable Interface (Recommended Way)

Instead of extending Thread, you can implement Runnable for better flexibility.

Step 1: Create a Runnable Class

package com.example.threads;

class RunnableThread implements Runnable {

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(Thread.currentThread().getName() + " - Count: " + i);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}
Step 2: Create Threads Using Thread Class

package com.example.threads;

public class ThreadDemo {

public static void main(String[] args) {

RunnableThread task = new RunnableThread();

Thread t1 = new Thread(task, "Thread 1");

Thread t2 = new Thread(task, "Thread 2");

t1.start(); // Start first thread

t2.start(); // Start second thread

6️ Key Differences: Thread vs Runnable

Feature Extending Thread Implementing Runnable

Inheritance Cannot extend another class Can extend other classes

Flexibility Less flexible More flexible

Best Practice Not recommended Recommended ✅

🔹 Use Runnable when possible because Java doesn’t support multiple inheritance.

7️ Summary of Steps

Step Action

1 Create a Java Project in Eclipse

2 Create a Package (com.example.threads)

3 Create a Thread Class (MyThread.java)

4 Write run() method in MyThread.java

5 Create ThreadDemo.java with main() method

6 Start threads using start()


Step Action

7 Run the program and see multithreading in action

You might also like