为什么输出都是null?

dlgz888 dlgz888 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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;
}
}
...全文
评论 17
收藏 已收藏
分享
返回顶部
dlgz888
Java SE 62,621 成员 307,251 内容