-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintNumAndArr.java
More file actions
81 lines (70 loc) · 2.09 KB
/
Copy pathprintNumAndArr.java
File metadata and controls
81 lines (70 loc) · 2.09 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package MultiThreadTest;
public class printNumAndArr {
static Object lock = new Object ();
public static void main (String[] args) {
TwoMethods tm = new TwoMethods ();
Thread ti = new Thread (new theInt (tm), "int");
ti.start ();
Thread tc = new Thread (new theChar (tm), "char");
tc.start ();
// theInt ti=new theInt (tm);
// theChar tc=new theChar (tm);
// new Thread (ti).start ();
// new Thread (tc).start ();
}
static class theInt implements Runnable {
TwoMethods tm;
public theInt (TwoMethods tm) {
super ();
this.tm = tm;
}
public void run () {
tm.showNum ();
}
}
static class theChar implements Runnable {
TwoMethods tm;
public theChar (TwoMethods tm) {
super ();
this.tm = tm;
}
public void run () {
tm.showChar ();
}
}
static class TwoMethods {
private int flag = 1;
public void showNum () {
for (int i = 1; i <= 26; i++) {
synchronized (lock) {
if (flag != 1) {
try {
lock.wait ();
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
System.out.print (i + ",");
flag = 0;
lock.notify ();
}
}
}
public void showChar () {
for (int i = 0; i < 26; i++) {
synchronized (lock) {
if (flag != 0) {
try {
lock.wait ();
} catch (InterruptedException e) {
e.printStackTrace ();
}
}
System.out.print ((char) (i + 'a') + ",");
flag = 1;
lock.notify ();
}
}
}
}
}