-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
67 lines (53 loc) · 2.01 KB
/
Copy pathMain.java
File metadata and controls
67 lines (53 loc) · 2.01 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
package blockchain;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
Blockchain blockchain = new Blockchain();
List<Man> manList = new ArrayList<>();
//矿工数
int minersCount = 20;
//需要的矿
int needBlockCount = 20;
//老板数
int bossCount = 10;
ExecutorService executors = Executors.newFixedThreadPool(minersCount);
int currentId = 1;
for (int i = 0; i < minersCount; i++) {
Miner miner = new Miner(blockchain, currentId++, needBlockCount, manList);
manList.add(miner);
executors.submit(miner);
}
for (int i = 0; i < bossCount; i++) {
List<Man> employeeList = new ArrayList<>();
Employee employee1 = new Employee(currentId++, manList, blockchain);
Employee employee2 = new Employee(currentId++, manList, blockchain);
Employee employee3 = new Employee(currentId++, manList, blockchain);
employeeList.add(employee1);
employeeList.add(employee3);
employeeList.add(employee2);
manList.add(employee1);
manList.add(employee2);
manList.add(employee3);
Boss boss = new Boss(currentId++, manList, employeeList, blockchain);
manList.add(boss);
employee1.setBoss(boss);
employee2.setBoss(boss);
employee3.setBoss(boss);
executors.submit(employee1);
executors.submit(employee2);
executors.submit(employee3);
executors.submit(boss);
}
try {
executors.shutdown();
while (!executors.isTerminated())
executors.awaitTermination(1000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
}