package Thread_case3_package; import java.util.ArrayList; import java.util.Collections; public class MyThread extends Thread { //集合:{10,5,20,50,100,200,500,800,2,80,300,700} ArrayList list; //线程一 static ArrayList list1 = new ArrayList<>(); //线程二 static ArrayList list2 = new ArrayList<>(); public MyThread(ArrayList list) { this.list = list; } @Override public void run() { while (true) { synchronized (Thread_case2_package.MyThread.class) { if (list.size() == 0) { if ("抽奖箱1".equals(getName())) { System.out.println("抽奖箱1" + list1); } else { System.out.println("抽奖箱2" + list2); } break; } else { //继续抽奖 Collections.shuffle(list); Integer prize = list.remove(0); //System.out.println(getName() + "又产生了一个" + prize + "元大奖"); if ("抽奖箱1".equals(getName())) { list1.add(prize); } else { list2.add(prize); } } } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }