终止在学习Java并发我就遇到了这个行为,我无法解释:的Java多线程的线程随机
public class ThreadInterferrence implements Runnable {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new ThreadInterferrence());
t.start();
append("1", 50);
t.join();
System.out.println(value);
}
private static String value = "";
public void run() {
append("2", 50);
}
private static void append(String what, int times) {
for (int i = 0; i < times; ++i) {
value = value + what;
}
}
}
程序为何生成随机字符串?更重要的是为什么输出长度不一?它不应该总是100个字符吗?
输出的例子:
22222222222222222222222222222222222222222222222222
1111111111111111111111111111112121112211221111122222222222222
等。
好吧,我跟着你到目前为止。但是为什么输出字符串的长度会变化?无论顺序如何,它不应该总是100个字符吗? – 2015-02-08 14:51:33