-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestThread.java
More file actions
40 lines (37 loc) · 794 Bytes
/
TestThread.java
File metadata and controls
40 lines (37 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class PrintDemo{
void printCount(){
for(int i=5 ;i>0 ;i--){
System.out.println("Counter --- " + i);
}
}
}
class ThreadDemo extends Thread{
private Thread t;
private String threadName;
PrintDemo PD;
ThreadDemo(String name, PrintDemo pd){
threadName = name;
PD = pd;
System.out.println("Starting " + threadName);
start();
}
public void run(){
synchronized(PD){
PD.printCount();
}
System.out.println("Thread " + threadName + " exiting.");
}
}
class TestThread{
public static void main(String[] args){
PrintDemo PD = new PrintDemo();
ThreadDemo T1 = new ThreadDemo( "Thread - 1", PD);
ThreadDemo T2 = new ThreadDemo( "Thread - 2", PD);
try {
T1.join();
T2.join();
} catch(InterruptedException e){
System.out.println("Interrupted");
}
}
}