Creating The Thread: 1. Extend Java - Lang.thread Inheritance. 1 Way
Creating The Thread: 1. Extend Java - Lang.thread Inheritance. 1 Way
4
Thread based multitasking
Thread is the smallest unit of code i. e. single program can
perform two or more tasks simultaneously.
Eg: Text editor can format the text at the same time printing other
job.
These two action can be performed by two separate threads.
Multithreading enables you to write efficient program that makes
maximum use of CPU.
Characteristics:=
It requires less overheads.
Threads are light weighted task.
They share same memory space.
Inter-thread communication is inexpensive.
Threads run at the same time but the flow of execution is shared
between thread.
5
Advantages of multithreading:
Reduces the computation time.
Improves performance of an application.
Threads share the same address space so it
saves the memory.
Context switching between threads is usually
less expensive than between processes.
Cost of communication between threads is
relatively low.
6
A thread can be in various
stages:
1. A thread can be running
2. A thread can be ready to run as soon as it
gets CPU time.
3. A running thread can be suspended (paused
explicitly)
4. A thread can be blocked.
5. A suspended thread can be resumed.
(explicitly)
6. At any time thread can be terminated.
7
Life cycle of a thread
During a life cycle of thread there are
following states in which it can enter:
1. New born state
2. Run-able or ready state
3. Running state
4. Block state
5. Dead state
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
8
Java thread model.
New born state
stop()
Start()
yield()
9
Methods that can be applied apply on a Thread:
Some Important Methods defined in java.lang.Thread are shown in the table:
Return
Method Description
Type
This method is the entry point to execute thread, like the main
run( ) void
method for applications.
10
Methods that can be applied apply on a Thread:
Some Important Methods defined in java.lang.Thread
are shown in the table:
sleep( ) void Suspends a thread for a specified amount of time (in milliseconds).
isAlive( ) boolean This method is used to determine the thread is running or not.
This method returns the number of active threads in a particular
activeCount( ) int
thread group and all its subgroups.
interrupt( ) void The method interrupt the threads on which it is invoked.
By invoking this method the current thread pause its execution
yield( ) void
temporarily and allow other threads to execute.
11
Example program1
// Controlling the main Thread.
class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
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); Output:
} Current thread: Thread[main,5,main]
After name change: Thread [My Thread,5,main]
}
catch (InterruptedException e) {
System.out.println("Main thread interrupted");
} 12
Creating the thread:
Java’s multithreading system is built upon
the Thread class, its methods and its
companion interface, Runnable.
A new thread can be created in two ways:
1. Program can implement java.lang.Runaable
interface.
2. Program can either extend java.lang.Thread
class.
13
Constructors for Thread:
Create a thread object using one of the following constructor:
Thread()
Allocates a new Thread object.
Thread(Runnable target)
Allocates a new Thread object.
Thread(Runnable target, String name)
Allocates a new Thread object.
Thread(String name)
Allocates a new Thread object.
14
Creating the thread:
Extend java.lang.Thread inheritance.
1st way
class t1 {
public static void main(String args[]){
dispthread dt1 = new dispthread("Hello");
dispthread dt2 = new dispthread("world");
dt1.start();
dt2.start(); }
}
class dispthread extends Thread
{
String msg;
public void run()
{
for(int i = 0;i<=3; i++)
{
System.out.println( msg);
}
}
dispthread (String m)
{ msg=m; }
} 15
Creating the thread:
Implement java.lang.Runaable interface.
2nd way
class t12 {
public static void main(String args[]){
dispthread dt1 = new dispthread("Hello");
dispthread dt2 = new dispthread("world");
Thread t1 = new Thread(dt1);
Thread t2 = new Thread(dt2);
t1.start();
t2.start();
} Output:
} Hello
class dispthread implements Runnable //extends Thread Hello
{ String msg; Hello
public void run() Hello
{ world
for(int i = 0;i<=3; i++)
world
{ System.out.println( msg);
}
world
} world
dispthread (String m)
{
msg=m;
} 16
}
Creating the thread:
1. implement java.lang.Runaable interface.
2nd way
class t12 {
public static void main(String args[]){
dispthread dt1 = new dispthread("Hello");
dispthread dt2 = new dispthread("world");
Thread t1 = new Thread(dt1);
Thread t2 = new Thread(dt2);
t1.start();
t2.start();
}
}
class dispthread implements Runnable
//extends Thread
Output:
{
String msg;
Hello
public void run()
{
Hello
for(int i = 0;i<=3; i++)
{ System.out.println( msg);
Hello
}
Hello
}
dispthread (String m)
world
{
msg=m;
world
}
} world
17
world
Multithreading
Creating multiple threads
Threads which we have used are
1. Main
2. Child thread
Program can produce as many threads
it needs.
Following program has three child
threads:
18
// Create multiple threads.
class NewThread implements Runnable class MultiThreadDemo {
{ public static void main(String args[]) {
String name; // name of thread new NewThread("One"); //start threads
Thread t; new NewThread("Two");
NewThread(String threadname) { new NewThread("Three");
name = threadname; try {
t = new Thread(this, name); // wait for other threads to end
System.out.println("New thread: " + t); Thread.sleep(10000);
t.start(); // Start the thread } catch (InterruptedException e) {
System.out.println("Main thread
}
Interrupted");
// This is the entry point for thread.
}
public void run() {
System.out.println("Main thread
try {
exiting.");
for(int i = 5; i > 0; i--) {
}
System.out.println(name + ": " + i);
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
} 19
Output:
/*
The output from this program is shown here: One: 1
New thread: Thread[One,5,main] Three: 1
New thread: Thread[Two,5,main] Two: 1
New thread: Thread[Three,5,main] One exiting.
One: 5 Two exiting.
Two: 5 Three exiting.
Three: 5 Main thread
One: 4 exiting.
Two: 4 */
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2 20