class Core {
private int value;
public int getSum() {
for (int i = 0; i < 50000000; i++) {
value++;
}
return value;
}
}
public class DemoRunnable implements Runnable{
@Override
public void run() {
Core core = new Core();
int sum = core.getSum();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sum);
}
}
public class Client {
public static void main(String[] args) {
DemoRunnable core = new DemoRunnable();
Thread one = new Thread(core);
Thread two = new Thread(core);
one.start();
two.start();
}
}
这段代码的输出结果为
50000000
50000000
为什么会有两行输出结果呢?虽然创建了两个线程,但是执行的是同一个Runnable的实例,那么应该只输出一个“50000000”才对吧?
紫衣仙女
相关分类