而如果我把run方法中的while(emp>0)换成while(true)来做,结果又不一样
public static void main(String[] args) {
Person p = new Person();
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
t1.start();
t2.start();
}
}
class Person implements Runnable{
static int emp = 100;
int num = 1;
int[] arrNum;
LinkedHashSet<Integer> hs = new LinkedHashSet<Integer>();
HashMap<String, Integer> hm = new HashMap<>();
String[] door = {"前门","后门"};
Random rd = new Random();
@Override
public void run() {
while(true){
synchronized (this) {
if(emp > 0){
while(hs.size() < 6){
hs.add(rd.nextInt(33)+1);
}
hs.add(rd.nextInt(16)+1);
String getDoor = door[rd.nextInt(2)];
if(getDoor.equals("前门")){
if(!hm.containsKey(getDoor)){
hm.put(getDoor, 1);
}else{
hm.put(getDoor, hm.get(getDoor)+1);
}
System.out.println("编号为:" + num + "的员工从" + getDoor + "入场! 拿到的双色球彩票号码是: " + hs);
}else if(getDoor.equals("后门")){
if(!hm.containsKey(getDoor)){
hm.put(getDoor, 1);
}else{
hm.put(getDoor, hm.get(getDoor)+1);
}
System.out.println("编号为:" + num + "的员工从" + getDoor + "入场! 拿到的双色球彩票号码是: " + hs);
}
// if(num == 100){
// System.out.println("前门" + "..." + hm.get("前门"));
// System.out.println("后门" + "..." + hm.get("后门"));
// return;
// }
emp--;
num++;
hs.clear();
}
}
}
}