Lecture 17 Multithreading
Lecture 17 Multithreading
Lecture 17
Multithreading
Multithreading
UNIT-III Multithreading
What is Multithreading?
Multithreading is similar to multi-processing.
A multi-processing Operating System can run
several processes at the same time.
UNIT-III Multithreading
What are Threads ?
Threads are lightweight processes as the
overhead of switching between threads is less.
UNIT-III Multithreading
What Kind of Applications Use
Multithreading?
Any application which requires asynchronous
response
Network based applications are ideally suited to
multithreading.
Data can from the network at any time.
In aarrive
single threaded system, data is queued until
the thread can read the data.
In a multithreaded system, a thread can be
dedicated to listening for data on the network port.
When data arrives, the thread reads it immediately
and processes it or delegates its processing to
another thread.
Thread Support in Java
notify()
Thread() start()
Created Runnable Blocked
sleep()
wait()
Dead
How does a Thread run?
The thread class has a run() method.
run() is executed when the thread's start() method is invoked.
Thread Object
run()
UNIT-III Multithreading
Creating threads
UNIT-III Multithreading
Using Runnable
In the example below, when the Thread object is
instantiated, it is passed a reference to a "Runnable"
object.
The Runnable object must implement a method called "run“.
run() run()
Running threads
Example
class Mythread implements Runnable{
public void run(){
System.out.println(“Thread Calling t. run () does
Started”);} not start a thread, it is
} just a simple method call.
class mainclass { Creating an object does not
public static void main(String args[]){ create a thread, calling
Thread t = new Thread(new start() method creates the
mythread( )); thread.
// This is the way to instantiate a
thread implementing runnable interface
t.start(); // starts the thread by running
the run method
}
}
Properly Terminating Threads
Class Demo
{
P.S.V.M(--)
{
Thread t=Thread.currentThread();
S.O.P(“Current Thread:” + t);
t.setName(“My Thread”);
S.O.P(“After name Change” + t);
try
{ for (n=5; n>0;n--)
{System.out.println(n);
Thread.sleep(1000);}
}catch(InterruptedException e)
{ S.O.P(“Main Thread
Interrupted”);}
}
}
Example (Creating Multiple Threads)
Class NT implements Runnable
{ Class Demo
{
String name; Thread t; P.S.V.M(--)
{
NT (String tname) NT one=new NT(“One”);
{
NT two=new NT(“Two”);
name=tname;
NT three=new NT(“Three”);
t=new Thread (this, try
name); S.O.P(“New {
Thread”); t.start(); Thread.sleep(10000);
}
}catch(InterruptedException
e)
public void run() { S.O.P(“Main Thread
{ Interrupted”);}
try
{ // some loop}catch(){ } S.O.P(“Main Thread Exiting”);
}
S.O.P(name +
}
“existing”);
}
}
Example (isAlive(), join())
Class NT implements Runnable Class Demo
{ {
String name; Thread t; P.S.V.M(--)
{
NT (String tname) NT one=new NT(“One”);
{ NT two=new NT(“Two”);
name=tname; NT three=new NT(“Three”);
t=new Thread (this, S.O.P(“Thread one is
name); S.O.P(“New Alive:”,one.t.isAlive();
Thread”); t.start(); try
} {
S.O.P(“Waiting for the Threads to Finish:”);
public void run() one.t.join();
{ two.t.join();
try three.t.join();
{ // some loop}catch(){ } }catch(InterruptedException e)
S.O.P(name + { S.O.P(“Main Thread Interrupted”);}
“existing”); S.O.P(“Main Thread Exiting”);
} }
} }
UNIT-III Multithreading
Thread Priorities
Every thread is assigned a priority (between 1 and 10).
The default is 5.
The higher the number, the higher the priority.
Can be set wit h setPriorit y( nt aPriority)
i .
UNIT-III g
Example (CreatingMultiple Threads)
class Clicker implements Runnable Class Demo
{ {
int click; Thread t; P.S.V.M(--)
private volatile Boolean rumming=true; {
Clicker (int p) Thread.currentThread().setPriority(10)
{ ; Clicker hi=new Clicker
t=new Thread (this); (Thread.NORM_PRIORITY+2);
t.setPriority(p); Clicker lo= new Clicker (3);
} lo.start();
public void run() hi.start();
{ try
while(rumming){ click++;} { Thread.sleep(10000);
} }catch() { }
lo.stop();
Public void stop() hi.stop();
{ running=false;} try{ hi.t.join();
lo.t.join();
Public void start() }catch() {}
{ t.start();} S.O.P(“Low Thread” + lo.click);
S.O.P(“High Thread” + hi.click);
}
}
}
Concurrent Access to Data
Those familiar with databases will understand
that concurrent access to data can lead to data
integrity problems.
Specifically, if two sources attempt to update