-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread1.java
51 lines (42 loc) · 935 Bytes
/
Thread1.java
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
40
41
42
43
44
45
46
47
48
49
50
51
package dummypkg;
public class Thread1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName());
Pair p=new Pair();
Thread one=new MYThread("one", p);
Thread two=new MYThread("two", p);
Thread three=new MYThread("three", p);
one.start();
two.start();
three.start();
}
}
class Pair {
int count=0;
}
class MYThread extends Thread{
Pair p;
public MYThread(String string, Pair p) {
super(string);
this.p=p;
}
@Override
public void run() {
// TODO Auto-generated method stub
func1();
}
public void func1() {
for(int i=0; i<5; i++) {
synchronized(p) {
p.notify();
try {
p.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " " + p.count++);
}
}
}
}