public class Test extends Thread {
private int threadNo;
private String lock;
public ThreadTest2(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String(“lock”);
for (int i = 1; i < 100; i++) {
new Test(i, lock).start();
}
}
public void run() {
synchronized (lock) {
for (int i = 1; i < 3; i++) {
System.out.println(“No.” + threadNo + “:” + i);
}
}
}
}作者: Fightin黑马 时间: 2014-9-11 16:48
谢谢分享