public class Test_01Demo extends Thread {
static int[] money = { 10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700 };
static int index = 0;
@Override
public void run() {
synchronized (Test_01.class) {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建随机数对象
Random r = new Random();
index = r.nextInt(money.length);
System.out.println(getName() + "又产生了一个" + money[index] + "元大奖");
}
}
}
}
// 测试类
public class Test_01 {
public static void main(String[] args) {
Test_01Demo t1 = new Test_01Demo();
t1.setName("抽奖箱1");
t1.start();
Test_01Demo t2 = new Test_01Demo();
t2.setName("抽奖箱2");
t2.start();
}
}