请完成如下需求:
有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池中的奖项为{10,5,20,50,100,200,500,800,2,80,300,700};
创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”,“抽奖箱2”随机从抽奖池中获取奖项元素并打印在控制台上,格式如下:
每次抽出一个奖项就打印一个(随机)
抽奖箱1又产生了一个10元大奖
抽奖箱1又产生了一个100元大奖
抽奖箱1又产生了一个200元大奖
抽奖箱1又产生了一个800元大奖
抽奖箱2又产生了一个700元大奖
.......
在此次抽奖过程中,抽奖箱1总共产生了6个奖项,分别为:10,20,100,500,2,300
最高奖项为300元,总计额为932元
在此次抽奖过程中,抽奖箱2总共产生了6个奖项,分别为:5,50,200,800,80,700
最高奖项为800元,总计额为1835元
在此次抽奖过程中,抽奖箱2中产生了最大奖项,该奖项金额为800元
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.TreeMap;
public class MyThread extends Thread{
Random r=new Random();
static TreeMap<String,Integer> map=new TreeMap<>();
static ArrayList<Integer> list=new ArrayList<>();
static {
Collections.addAll(list,10,5,20,50,100,200,800,2,80,300,700);
}
//static Object lock=new Object();
@Override
public void run() {
ArrayList<Integer> boxList=new ArrayList<>();
while(true){
synchronized (MyThread.class){
if(list.size()>0){
int index = r.nextInt(list.size());
int number=list.get(index);
System.out.println(getName()+"又产生了一个"+number+"元大奖");
list.remove(index);
boxList.add(number);
}else {
System.out.println(getName()+boxList);
System.out.println(getName()+"最大值"+Collections.max(boxList));
map.put(getName(),Collections.max(boxList));
int sum=0;
for (Integer integer : boxList) {
sum=sum+integer;
}
System.out.println(getName()+"和"+sum);
System.out.println(map.firstEntry().getKey()+"抽到了最大奖,奖金是:"+map.firstEntry().getValue()+"元");
break;
}
}
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] args) {
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.setName("抽奖箱1");
t2.setName("抽奖箱2");
t1.start();
t2.start();
}
}