File size: 1,203 Bytes
0284885 | 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 | package Thread_case5_package;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;
public class MyCallable implements Callable<Integer> {
ArrayList<Integer> list;
public MyCallable(ArrayList<Integer> list) {
this.list = list;
}
@Override
public Integer call() throws Exception {
ArrayList<Integer> boxList = new ArrayList<>();
while (true) {
synchronized (Thread_case2_package.MyThread.class) {
if (list.size() == 0) {
System.out.println(Thread.currentThread().getName() + boxList);
break;
} else {
//继续抽奖
Collections.shuffle(list);
Integer prize = list.remove(0);
//System.out.println(getName() + "又产生了一个" + prize + "元大奖");
boxList.add(prize);
}
}
Thread.sleep(10);
}
//把集合中的最大值返回
if (boxList.size() == 0) {
return null;
} else {
return Collections.max(boxList);
}
}
}
|