forked from CentMeng/JavaFrameTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
38 lines (31 loc) · 999 Bytes
/
Copy pathMain.java
File metadata and controls
38 lines (31 loc) · 999 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
package com.msj.sync.masterworker;
import java.util.Random;
/**
* @author Vincent.M mengshaojie@188.com
* @date 2017/3/15 下午3:25
* @copyright ©2017 孟少杰 All Rights Reserved
* @desc Master-Worker模式
*/
public class Main {
public static void main(String[] args) {
System.out.println("我的机器java虚拟机可用线程数量"+Runtime.getRuntime().availableProcessors());
Master master = new Master(new Worker(), Runtime.getRuntime().availableProcessors()+1);
Random r = new Random();
for(int i = 1; i <= 100; i++){
Task t = new Task();
t.setId(i);
t.setPrice(r.nextInt(1000));
master.submit(t);
}
master.execute();
long start = System.currentTimeMillis();
while(true){
if(master.isComplete()){
long end = System.currentTimeMillis() - start;
int priceResult = master.getResult();
System.out.println("最终结果:" + priceResult + ", 执行时间:" + end);
break;
}
}
}
}