0% found this document useful (0 votes)
5 views9 pages

Lecture - Multithreading

This document introduces multi-threading in Java, explaining what threads are and their importance for improving program efficiency and responsiveness. It outlines two main methods for creating threads: by extending the Thread class and by implementing the Runnable interface, providing examples for both approaches. The document also discusses when to use each method and highlights the significance of threads in real-world applications such as games and web servers.

Uploaded by

nirajandey2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Lecture - Multithreading

This document introduces multi-threading in Java, explaining what threads are and their importance for improving program efficiency and responsiveness. It outlines two main methods for creating threads: by extending the Thread class and by implementing the Runnable interface, providing examples for both approaches. The document also discusses when to use each method and highlights the significance of threads in real-world applications such as games and web servers.

Uploaded by

nirajandey2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multi-threading : Part 1

Dr. Nitin Varyani


Introduction to Threads in Java
• What is a Thread?
• A thread is a lightweight unit of execution in a program.
• Think of threads as workers that perform tasks in parallel within your
program.
• Why use threads?
• To run multiple tasks at the same time, improving efficiency.
• To make programs more responsive, especially in complex
applications like games or web servers.
Two Ways to Create Threads in Java
There are two main ways to create threads in
Java:
[Link] extending the Thread class.
[Link] implementing the Runnable interface.
Creating a Thread by Extending the Thread
Class
Steps:
[Link] a class that extends the Thread class.
[Link] the run() method to define the task the thread should perform.
[Link] an object of your class and call the start() method of Thread class to
begin execution.
class MyThread extends public class Main {
Thread { public static void main(String[] args) {
@Override MyThread thread = new MyThread();
public void run() { // Create the thread
// Task performed by the [Link]();
thread // Start the thread to execute its task
for (int i = 0; i <= 25; i++) { }
[Link](i); }
}}}
Multi-threading by extending the thread class
class MyThread extends Thread { class Thread1 extends Thread {
public void run() { public void run() {
// Task logic here // Task 1 logic here
} }
} }
class Thread2 extends Thread {
public class Main { public void run() {
public static void main(String[] // Task 2 logic here
args) { }
MyThread t1 = new MyThread(); }
MyThread t2 = new MyThread(); public class Main {
[Link](); public static void main(String[]
[Link](); args) {
} Thread1 t1 = new Thread1();
} Thread2 t2 = new Thread2();
[Link]();
[Link]();
}
}
Multi-threads for same class Multi-threads for different classes
Creating a Thread by Implementing the
Runnable Interface
Steps:
[Link] a class that implements the Runnable interface.
[Link] the run() method where you define the task.
[Link] a Thread object by passing the Runnable object to it.
[Link] the start() method of Thread class to begin execution.

class MyRunnable implements Runnable public class Main {


{ public static void main(String[] args) {
@Override MyRunnable task = new MyRunnable(); // Create the
public void run() { task
// Task performed by the thread Thread thread = new Thread(task); // Create the thread
for (int i = 0; i <= 25; i++) { [Link](); // Start the thread to execute its task
[Link](i); }
} }
}}
Multi-threading by Implementing Runnable Inter
class MyRunnable1 implements Runnable {
class MyRunnable implements Runnable { @Override
@Override public void run() {
public void run() { // Task 1 logic here
// Task logic here }}
} class MyRunnable2 implements Runnable {
} @Override
public void run() {
public class Main { // Task 2 logic here
public static void main(String[] args) { }}
MyRunnable task = new MyRunnable(); public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(task); MyRunnable1 task1 = new MyRunnable1();
[Link](); // Start Task 1 Thread t1 = new Thread(task1);
[Link](); // Start Task 1
Thread t2 = new Thread(task);
[Link](); // Start Task 2 MyRunnable2 task2 = new MyRunnable2();
} Thread t2 = new Thread(task2);
} [Link](); // Start Task 2
}}
Multi-threads for same class Multi-threads for different classes
When to Use Which Approach?
• Use Extending Thread:
• If you want to create a thread and don't need to extend any other
class besides Thread.
• Use Implementing Runnable:
• If you need to extend another class besides Thread class.
class Car extends Vehicle implements Runnable {
@Override
public void run() {
[Link](“Car thread is running..."); }
}
Extend another class besides Thread
Why Threads Are Important?
• Concurrency: Threads allow you to run multiple tasks at the same
time.
• Efficiency: Using threads can make better use of CPU by running
multiple operations concurrently.
• Real-World Applications: Threads are used in games, real-time
applications, and web servers to perform tasks like downloading,
rendering, or handling multiple users simultaneously.

You might also like