test4 t1 = new test4(); test4 t2 = new test4(); test4 t3 = new test4(); test4 t4 = new test4(); test4 t5 = new test4(); t1.setName("张三"); t2.setName("李四"); t3.setName("王五"); t4.setName("赵六"); t5.setName("李逵"); t1.start(); t2.start(); t3.start(); t4.start(); t5.start();
public class test4 extends Thread { //金额 static double money = 100; //人数 static int count = 3; //最小金额 static final double MIN = 0.01; @Override public void run() { synchronized (test4.class) { if (count == 0) { System.out.println(getName() + "没有抢到红包"); } else { //定义中奖金额 double price = 0; if (count == 1) { //不再随机 price = money; } else { //进行随机 Random r = new Random(); //金额是小数 //100-(3-1)*0.01 double bounds = money - (count - 1) * MIN; //随机出来的红包价值 price = r.nextDouble(bounds); //判断比最小金额 if (price < MIN) { price = MIN; } } //金额减去抽到的金额 money=money-price; count--; System.out.println(getName()+"抢到了"+price+"元"); } } } }