-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHRAndInterviewer.java
116 lines (101 loc) · 3.26 KB
/
HRAndInterviewer.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package multithreading;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* @Author: Wenhang Chen
* @Description:
* @Date: Created in 15:07 4/7/2020
* @Modified by:
*/
public class HRAndInterviewer {
private static final Object lock = new Object();
private static final int ALL = 1000;
private static final int FULL = 100;
private static int num = 0;
private static int count = 0;
// HR
static class HR implements Runnable{
@Override
public void run(){
while(count<ALL){
try{
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
}catch(Exception e){
e.printStackTrace();
}
synchronized(lock){
while(num==FULL){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num++;
count++;
System.out.println("HR处理简历");
lock.notifyAll();
}
}
}
}
// 面试官A
static class InterviewA implements Runnable{
@Override
public void run(){
while(count<ALL){
try{
TimeUnit.SECONDS.sleep(3);
}catch(Exception e){
e.printStackTrace();
}
synchronized(lock){
while(num==0){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num--;
int flag = new Random().nextInt(1);
if (flag == 1) System.out.println("面试官A处理简历 成功");
if (flag == 0) System.out.println("面试官A处理简历 失败");
lock.notifyAll();
}
}
}
}
// 面试官B
static class InterviewB implements Runnable{
@Override
public void run(){
while(count<ALL){
try{
TimeUnit.SECONDS.sleep(3);
}catch(Exception e){
e.printStackTrace();
}
synchronized(lock){
while(num==0){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num--;
int flag = new Random().nextInt(1);
if (flag == 1) System.out.println("面试官B处理简历 成功");
if (flag == 0) System.out.println("面试官B处理简历 失败");
lock.notifyAll();
}
}
}
}
public static void main(String[] args){
new Thread(new HR()).start();
new Thread(new InterviewA()).start();
new Thread(new InterviewB()).start();
}
}