private int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
private int num = arr.length;
private ArrayList<Integer> list = new ArrayList<Integer>();
@Override
public void run() {
String threadName = Thread.currentThread().getName();
Random r = new Random();
while(true) {
synchronized(this){
if(num==0)
break;
//创建随机数对象
int index = r.nextInt(arr.length);
//如果随机数已经被取过一次,那么本次循环结束继续下次循环
if(list.contains(index)){
continue;
}
//为了获取不重复的索引值
int money = arr[index];
//将随机数存入集合中
list.add(index);
System.out.println(threadName + " 又产生了一个 "+money+" 元大奖");
num--;
}
}
}
} |