[Java] 纯文本查看 复制代码
public class Lucky {
public static void main(String[] args) {
MyRunnable my=new MyRunnable();
Thread t1 = new Thread(my,"抽奖箱1");
Thread t2=new Thread (my,"抽奖箱2");
t1.start();
t2.start();
}
}
[Java] 纯文本查看 复制代码
import java.util.Random;
public class MyRunnable implements Runnable {
private int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
public void run() {
while(true){
get();
}
}
private synchronized void get() {
Random r=new Random();
int x=r.nextInt(arr.length);
System.out.println(Thread.currentThread().getName()+"又产生了一个"+arr[x]+"元大奖");
try {
//为方便查看控制台加入200ms时间间隔
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}