java一个类里面新建几个线程,怎样把每个线程收集的数据放在一个数组里面?????、、、、、、、、小生初学请各位长辈指点

解决方案 »

  1.   

    为什么放数组里面?你能确定数组的长度吗?可以用hashtable,而且是线程同步的。
    不一定非得是静态的,可在构造时把引用传进去。
      

  2.   

    vetor也不是不行啊,map不一定适合这个楼主的需求。
      

  3.   

    看代码:
    public class MyJava{
    public static void main(String[] args){
    String[] strs=new String[2];
    MyThread_A t1=new MyThread_A(strs);
    MyThread_B t2=new MyThread_B(strs);
    t1.start();
    t2.start();
    for(String s:strs) System.out.println(s); //打印每个线程收集的数据
    }
    }class MyThread_A extends Thread{
    String[] strs;
    public MyThread_A(String[] strs){
    this.strs=strs;
    }
    public void run(){
    String s=null; //计算机strs[0]的值
    s="a";
    strs[0]=s;
    }
    }class MyThread_B extends Thread{
    String[] strs;
    public MyThread_B(String[] strs){
    this.strs=strs;
    }
    public void run(){
    String s=null;
    s="b"; //计算机strs[1]的值
    strs[1]=s;
    }
    }