0% found this document useful (0 votes)
27 views12 pages

THREADS

Threads

Uploaded by

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

THREADS

Threads

Uploaded by

Sneha Nenu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

THREADS

What is Thread

• Thread is a light weight process

• Java provides built in support for multithreading programming

• A multithreaded program contains two or more parts that can run concurrently.
Each part of that program is called thread.

• Each thread define a separate path of execution.

• The thread is a dispatchable code. This means that a single program can
perform two or more program task simultaneously
Uses of threads

 Maximum utilization of processor

 Eg: if we want to add 800 elements {1,2,3,….800}

p1 p2

p3 p4
 Web Application
 Gaming
How to create thread

 Extending thread

 Implements runnable
Extending Thread

// Create a second thread by extending


Thread
// This is the entry point for the second
class NewThread extends Thread { thread.
NewThread() { public void run() {
// Create a new, second thread try {
super("Demo Thread"); for(int i = 5; i > 0; i--) {
System.out.println("Child thread: " + System.out.println("Child Thread: " + i);
this);
Thread.sleep(500);
start(); // Start the thread
}
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}System.out.println("Exiting child thread.");
}}
class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
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.");
}}
Using implement the Runnable
interface
// This is the entry point for the second
class NewThread implements Runnable { thread.
Thread t; public void run() {

NewThread() { try {
for(int i = 5; i > 0; i--) {
// Create a new, second thread
System.out.println("Child Thread: " + i);
t = new Thread(this, "Demo
Thread.sleep(500);
Thread");
}
System.out.println("Child
} catch (InterruptedException e) {
thread: " + t);
System.out.println("Child interrupted.");
t.start(); // Start the thread
}
}
System.out.println("Exiting child thread.");
}}
class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
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.");
}}
Important methods

Method Meaning
getName Obtain a thread’s name.
getPriority Obtain a thread’s priority.
isAlive Determine if a thread is still running.
join Wait for a thread to terminate.
run Entry point for the thread.
sleep Suspend a thread for a period of time.
start Start a thread by calling its run method.
Thank you

You might also like