0% found this document useful (0 votes)
1 views10 pages

Thread

Uploaded by

nabiha.fiza
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)
1 views10 pages

Thread

Uploaded by

nabiha.fiza
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

Introduction to Programming Language II (Java)

Nabiha Fiza
Lecturer, CSE | Southeast University

1
Thread in Java

❑ A thread in Java is a lightweight unit of execution that runs independently within a program.
❑ It allows multitasking, where multiple parts of a program run concurrently, improving performance and
responsiveness.

❑ Threads allows a program to operate more efficiently by doing multiple things at the same time.
❑ Threads can be used to perform complicated tasks in the background without interrupting the main program.

public class main {


public static void main(String[] args) {
[Link]("Main thread running");
}
}
start() and run() Methods

start() Method
❑ Creates a new thread and starts its execution.
❑ Internally calls the run() method.
❑ Defined only in Thread Class

run() Method
❑ Contains the task/code that a thread will execute.
❑ It is not responsible for creating a new thread.
❑ Defined in the Thread class and Runnable interface.
Thread in Java
Creating a Thread
There are two ways to create a thread.
1. By extending the Thread class and overriding its run() method
2. Create a thread is to implement the Runnable interface

Extend Syntax
public class Main extends Thread {
public void run() {
[Link]("This code is running in a thread");
}
}

Implement Syntax
public class Main implements Runnable {
public void run() {
[Link]("This code is running in a thread");
}
}
❑ Differences between "extending" and "implementing" Threads

The major difference is that when a class extends the Thread class, you cannot extend any other class,
but by implementing the Runnable interface, it is possible to extend from another class as well,
like: class MyClass extends OtherClass implements Runnable.
Running Threads
1. If the class extends the Thread class, the thread can be run by creating an instance of the class and call
its start() method:

Extend Example
public class Main extends Thread {
public static void main(String[] args) {
Main thread = new Main();
[Link]();
[Link]("This code is outside of the thread");
}
public void run() {
[Link]("This code is running in a thread");
}
}
[Link] the class implements the Runnable interface, the thread can be run by passing an instance of the class to
a Thread object's constructor and then calling the thread's start() method:
Example:
class PrintTask implements Runnable {
public void run() { Pauses the current executing thread for 400 milliseconds.
for (int i = 1; i <= 3; i++) { During this pause, CPU can switch to another thread.
[Link]("Printing document " + i);
try {
Catches the exception if the thread is
[Link](400);
interrupted while sleeping.
} catch (InterruptedException e) {
[Link](e);
}
} Output:
} (order may be different but each line 2 times)
Printing document 1
public static void main(String[] args) { Printing document 1
Thread t1 = new Thread(new PrintTask()); Printing document 2
Thread t2 = new Thread(new PrintTask()); Printing document 2
Printing document 3
[Link](); Printing document 3
[Link]();
}
}

You might also like