[Java] 纯文本查看 复制代码 package com.exercise.ss;
import java.util.Random;
public class Test7 {
/**
* @param args
*
*/
public static void main(String[] args) {
myRunnable mr = new myRunnable();
Thread th1 = new Thread(mr);
Thread th2 = new Thread(mr);
th1.setName("恒大");
th2.setName("鲁能");
th1.start();
th2.start();
}
}
class myRunnable implements Runnable{
private String str[] ={"世界杯" , "亚洲杯" ,"欧洲杯" , "美洲杯" , "奥布莱恩杯"};
private int num = 1;
private Random r = new Random();
@Override
public void run() {
while(num <= 10){
synchronized (this) {
int i = r.nextInt(5);
System.out.println(Thread.currentThread().getName() + "捧起了" + str[i]);
num++;
}
}
}
}
|