Thread
Thread
Multithreaded Programming
Incheon Paik
Contents
An Overview of Threads
Creating Threads
Synchronization
Deadlock
Thread Communication
What is a Thread?
•A sequence of execution within a process
•A Lightweight process
•JVM manages and schedules threads
•Possible States:
(1) new (2) ready (3) running (4) waiting (5) dead
An Overview of Threads
Threads
Dead
Sleep,wait,I/O
Methods in a Thread
Static Methods of the Thread Class Instance Methods of Thread
class ThreadDemo1 {
public static void main(String args[]) {
ThreadX tx = new ThreadX();
tx.start();
}
}
Creating Threads
class JoinDemo1 {
class ThreadM extends Thread {
public static void main(String args[]) {
public void run() {
try {
ThreadM tm = new ThreadM();
for (int i = 0; i < 10; i++) {
tm.start();
Thread.sleep(1000);
ThreadN tn = new ThreadN();
System.out.println("ThreadM");
tn.start();
}
try { join() method:
}
tm.join(); Waits for this thread to die.
catch (InterruptedException ex) {
tn.join();
ex.printStackTrace();
System.out.println("Both threads have
} finished");
} }
} catch (Exception e) {
e.printStackTrace();
class ThreadN extends Thread { }
public void run() { }
try { }
for (int i = 0; i < 20; i++) {
Thread.sleep(2000);
System.out.println("ThreadN");
}
}
catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
8 Computer Industry Lab.
Java
Synchronization
Thread Scheduling
Synchronized Block
Synchronized (obj) {
// Process Block
}
If a thread invokes a
synchronized method, the
object will be locked. If
other threads invoke the
synchronized method of
that object, the threads
will be blocked.
Data Corruption!
Need Synchronization
run() { run() {
run() {
obj1.method3(); obj2.method3();
obj1.method2();
obj1.method1(); obj2.method2();
}
obj2.method1(); }
}
1
2 4 5
3 OK. 6
method1()
Not busy
obj 1 obj 2
synchronized synchronized
method1() method1()
No!
synchronized synchronized
Not while method2()
method2() for obj1 is executing method2()
OK.
method2() method3() method3()
Not busy Always OK. No! Always OK.
Not while method1()
for obj2 is executing
10 Computer Industry Lab.
Java
Synchronization
class Account { class BankDemo {
private int balance = 0; private final static int NUMCUSTOMERS = 10;
Deadlock
class A { public void run() { // Create threads
B b; for (int i = 0; i < 100000; i++) Thread1 t1 = new Thread1(a);
a.a1(); Thread2 t2 = new Thread2(b);
synchronized void a1() { } t1.start();
System.out.println("Starting a1"); t2.start();
b.b2(); }
} // Wait for threads to complete
class Thread2 extends Thread { try {
synchronized void a2() { B b; t1.join();
System.out.println("Starting a2"); t2.join();
} Thread2(B b) { }
} this.b = b; catch(Exception e) {
} e.printStackTrace();
class B { }
A a;
public void run() { //Display Message
synchronized void b1() { for (int i = 0; i < 100000; i++) System.out.println("Done!");
System.out.println("Starting b1"); b.b1(); }
a.a2(); } }
} }
Condition for Deadlock:
synchronized void b2() { class DeadlockDemo { - Mutual Exclusion - Hold & Wait
System.out.println("Starting b2");
} - No-preemption - Circular Wait
public static void main(String args[])
} {
notifyAll() Method
void notifyAll()
“Form”
synchronized(theObject) {
// statement block
}
No other statements or statements blocks in the program that are synchronized on the object can
execute while this statement is executing.
Exercise