public class D10多线程_共享数组添加3 {
public static void main(String[] args) {
new myth().start();
new myth().start();
}
}
class myth extends Thread {
static int i = 0; //必须是静态的,要不然,每个对象就自建一个
static int[] arr = new int[6];
public void run() {
while (true) {
synchronized (myth.class) {
if (i >= 6) {
break;
}
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
arr[i] = i;
i++;
}
System.out.println("第" + i + "次添加 , 线程" + this.getName() + "向数组"
+ arr + "进行添加操作, 添加后, 数组各个值为" + arr[0] + "___" + arr[1]
+ "___" + arr[2] + "___" + arr[3] + "___" + arr[4] + "___"
+ arr[5]);
}
}
} |
|