class Play5 {
public static void main(String[] args) {
Window w = new Window();
new Thread(w, "窗口1").start();
new Thread(w, "窗口2").start();
new Thread(w, "窗口3").start();
new Thread(w, "窗口4").start();
}
}
class Window implements Runnable {
int[] arr = new int[20];
int i =0;
Object lock = new Object();
public void run() {
while (i<=20) {
synchronized (lock){
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (i==20) {
for (int j =0;j<=19 ;j++ ) {
System.out.print(arr[j]+" ");
}
i++;
break;
}else if (i>20) {
break;
}
Thread t = Thread.currentThread();
arr[i]= (int)(100*Math.random())+1;
System.out.println(t.getName() +"往数组里放入"+ arr[i++]);
}
}
}
}
|
|