本帖最后由 在学野马 于 2014-10-7 19:48 编辑
public class Share implements Runnable{
private static int[]arr=new int[6];
private static int next=0;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new Thread(new Share()).start();
new Thread(new Share()).start();
Thread.sleep(2000);
for(int i:arr){
System.out.print(" "+i+"\t");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(500);
synchronized(Share.class){
for(int i=0;i<3;i++){
int number=(int) (Math.random()*10+1);
arr[next]=number;
System.out.println(arr[next]);
next++;
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
不能是主线程随机产生 两个子线程再分享他吗 还有那个arr和next
}
|
|