Multithreaded Programming Using Java Threads
Multithreaded Programming Using Java Threads
Web/Internet Applications:
Serving Many Users Simultaneously
PC client
Internet
Server
Local Area Network
PD
A
2
Editing Thread
..
}
beginning
Single-threaded body
of execution
end
A Multithreaded Program
Main Thread
start
Thread A
start
Thread B
start
Thread C
Contd..
Benefits
11
Newborn state
Runnable state
Running state
Blocked state
Dead state
Active
Thread
stop
Runnable
Running
stop
Killed
Thread
yield
Suspend
resume
notify
Sleep
Dead
stop
wait
Idle Thread
(Not
Runnable)
Blocked
13
Newborn state
Newborn
start
Runnable state
stop
Dead State
Runnable state
Running
Thread
Runnable threads
Running State
Suspend
resume
Running
Runnable
suspended
Sleep(t)
After t
Running
Runnable
sleeping
wait
notify
Running
Runnable
waiting
Blocked state
Blocking a thread
sleep()
suspend()
wait()
These methods cause the thread to go into the blocked state (or not
runnable) state.
Sleep() : when specified time is elapsed
Suspend(): resume() method is invoked
Wait(): notify() method is invoked
Stopping a thread
Creating threads
24
Threading Mechanisms...
Thread
MyThread
Runnable
Thread
MyClass
Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
//invokes run() method
Create and Execute:
new MyThread().start();
26
An
example
class MyThread extends Thread
{
class ThreadEx1
{
public static void main (String [] args )
{
MyThread t = new MyThread();
t.start();
}
}
27
An example
class MyThread implements Runnable
{
public void run()
{
System.out.println(" this thread is running ... ");
}
}
class ThreadEx2
{
public static void main (String [] args )
{
Thread t = new Thread(new MyThread());
t.start();
}
}
29
30
31
Run 1
Javac ThreadTest.java
java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
Exit from A
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
33
Run2
[raj@mundroo] threads [1:77] java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
Exit from A
34
Example 2
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1) yield();
System.out.println("\t From Thread A: i= "+i);
}
System.out.println("Exit from A");
}}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\t From Thread B: j= "+j);
if (j==3) stop();
}
System.out.println("Exit from B");
}
}
35
36
class ThreadMethods
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println("Start Thread A");
threadA.start();
System.out.println("Start Thread B");
threadB.start();
System.out.println("Start Thread C");
threadC.start();
37
Start thread A
Start thread B
Start thread C
From thread B: j=1
From thread B: j=2
From thread B: j=3
From thread A: i=1
From thread A: i=2
End of main thread
From thread C: k=1
From thread A: i=3
From thread A: i=4
From thread A: i=5
Exit from A
From thread C: k=2
From thread C: k=3
From thread C: k=4
From thread C: k=5
Exit from C
38
39
40
Contd..
While isAlive( ) is occasionally useful, the method
that you will more commonly use to wait for a thread
to finish is called join( ), shown here:
final void join( ) throws InterruptedException
This method waits until the thread on which it is called
terminates.
Its name comes from the concept of the calling thread waiting
until the specified thread joins it.
41
Thread Priority
ThreadName.setPriority(intNumber)
MIN_PRIORITY = 1
NORM_PRIORITY=5
MAX_PRIORITY=10
42
43
Start thread A
Start thread B
Start thread C
threadB started
From ThreadB: j= 1
From ThreadB: j= 2
threadC started
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
Exit from C
End of main thread
From ThreadB: j= 3
From ThreadB: j= 4
Exit from B
threadA started
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 2
From ThreadA: i= 2
Exit from A
Run 1
45
Deposit()
Withdraw()
Enquire()
46
Internet Bank
Server
Local Area Network
Bank
Database
PD
A
47
class Callme
{
void call(String msg)
{
System.out.print("[" + msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
48
class Synch {
public static void main(String args[])
{
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
49
50
Solution 1
class Callme {
synchronized void call(String msg) {
...
}
}
After synchronized has been added to call( ), the
output of the program is as follows:
[Hello]
[Synchronized]
[World]
51
Solution 2
put calls to the methods defined by this class inside a synchronized
block.
This is the general form of the synchronized statement:
synchronized(object) {
// statements to be synchronized
}
Here, object is a reference to the object being synchronized. A
synchronized block ensures that a call to a method that is a member
of object occurs only after the current thread has successfully
entered objects monitor.
52
class Callme
{
void call(String msg)
{
System.out.print("[" + msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Synch {
public static void main(String args[])
{
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
// wait for threads to end
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
54
Comparison
55