0% found this document useful (0 votes)
29 views13 pages

4-Multithreaded Programming-22Aug24

Uploaded by

22pw03
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)
29 views13 pages

4-Multithreaded Programming-22Aug24

Uploaded by

22pw03
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

Chapter - 4

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 = [Link]();
[Link]("Current thread: " + t);
[Link]("My Thread");
[Link]("After name change: " + t);
try {
for(int n = 5; n > 0; n--) {
[Link](n);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("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");
[Link]("Child thread: " + t);
[Link]();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
[Link]("Child Thread: " + i);
[Link](500);
}
} catch (InterruptedException e) {
[Link]("Child interrupted.");
}
[Link]("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--) {
[Link]("Main Thread: " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Main thread interrupted.");
}
[Link]("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");
[Link]("Child thread: " + this);
start();
}
public void run() {
try {
for(int i = 5; i > 0; i--) {
[Link]("Child Thread: " + i);
[Link](500);
}
} catch (InterruptedException e) {
[Link]("Child interrupted.");
}
[Link]("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--) {
[Link]("Main Thread: " + i);
[Link](1000);
}
} catch (InterruptedException e) {
[Link]("Main thread interrupted.");
}
[Link]("Main thread exiting.");
}
}

12
References
 [Link]
 [Link]
 [Link]
 [Link]
 [Link]
 [Link]
 [Link]
[Link]/

13

You might also like