4-Multithreaded Programming-22Aug24
4-Multithreaded Programming-22Aug24
MULTITHREADED
PROGRAMMING
(cont..)
1
Topics of today!!!
Main Thread
Creating a Thread
2
Main Thread
Java program starts up, one thread begins running
immediately is called main thread
The main thread is important for two reasons
It is the thread from which other “child” threads will
be spawned
It must be the last thread to finish execution because
it performs various shutdown actions
3
Main Thread - Example
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}
4
Creating a Thread
Create a thread by instantiating an object of
type Thread
Two ways in which this can be accomplished
Implement the Runnable interface
Extend the Thread class
5
Creating a Thread (cont..)
Runnable interface
implemented by any class that will initiate
a separate thread of execution
defines one abstract method, called run( ),
which is the entry point to the thread
Syntax
public void run( )
6
Creating a Thread (cont..) - Runnable
class NewThread implements Runnable {
Thread t;
NewThread() {
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
7
}
Creating a Thread (cont..) - Runnable
class ThreadDemo {
public static void main(String args[ ] ) {
new NewThread();
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
8
Creating a Thread (cont..)
Thread class
creates a new thread of execution and it implements
Runnable
defines the following commonly used constructors
Thread( )
Thread(Runnable threadOb)
Thread(Runnable threadOb, String threadName)
Thread(String threadName)
Thread(ThreadGroup groupOb, Runnable threadOb)
Thread(ThreadGroup groupOb, Runnable threadOb, String threadName)
Thread(ThreadGroup groupOb, String threadName)
9
Creating a Thread (cont..)
Thread class - methods
10
Creating a Thread (cont..) - Thread
class NewThread extends Thread {
NewThread() {
super("Demo Thread");
System.out.println("Child thread: " + this);
start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
} 11
Creating a Thread (cont..) - Thread
class ExtendThread {
public static void main(String args[]) {
new NewThread();
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
12
References
https://www.tutorialspoint.com/java/java_multithreading.htm
https://www.javatpoint.com/multithreading-in-java
https://www.geeksforgeeks.org/multithreading-in-java/
http://tutorials.jenkov.com/java-concurrency/index.html
https://www.journaldev.com/1079/multithreading-in-java
https://beginnersbook.com/2013/03/multithreading-in-java/
https://www.scientecheasy.com/2020/08/life-cycle-of-
thread-in-java.html/
13