多线程问题,不明白结果

neary neary 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class TestThread extends Object implements Runnable
{
String s1 = "Earth";

String s2 = "Moon";

public void run()
{
synchronized (s1)
{
for (int i = 0; i < 2; i++)
{
System.out.println(Thread.currentThread().getName());
s1.concat("to Moon");
System.out.println(s1);
s2.concat("to Earth");
System.out.println(s2);
}
}
}

public static void main(String[] args)
{
//System.out.println("Earch".concat("to Moon"));
TestThread t = new TestThread();
new Thread(t).start();
new Thread(t).start();
}
}
/*
结果:
Thread-0
Earth
Moon
Thread-0
Earth
Moon
Thread-1
Earth
Moon
Thread-1
Earth
Moon
*/
...全文
评论 3
收藏 已收藏
分享
返回顶部
neary
Java SE 62,621 成员 307,251 内容