多线程问题,不明白结果
neary 2005-11-18 04:03:30 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
*/