/**
* 声明一个共享数组,起两个线程,两个线程分别隔一段时间(可以写一个随机数),
* 给数组中添加数据,每一个线程为数组添加3个数据即可。
* @author Administrator
*
*/
public class Thread2{
public static void main(String[] args) {
String[] share = new String[6];
Arr t2 = new Arr(share);
Thread t = new Thread(t2);
Thread t1 = new Thread(t2);
t.start();
t1.start();
for (String string : share) {
System.out.println(string);
}
}
}
class Arr implements Runnable {
public boolean flag= true;
private String[] share = new String[6];
public Arr(String[] share) {
this.share= share;
}
@Override
public void run() {
int num=0;
synchronized (share) {
for (int i = 0; i < 3; i++) {
num++;
share[num]= Thread.currentThread().getName()+i+"///";
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
请教大神,为啥我数组中有三个元素为null。。。应该怎么写
|