线程类问题求解
问题如下:
24. What will be the output on compiling/running the following code?
public class MyThread implements Runnable {
String myString = "Yes ";
public void run() {
this.myString = "No ";
}
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t).start();
for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
我的答案是C
标答是E
解释如下:
24. E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.
没看明白,难道线程不是独立于平台的吗?谁能详细解释一下?
24. What will be the output on compiling/running the following code?
public class MyThread implements Runnable {
String myString = "Yes ";
public void run() {
this.myString = "No ";
}
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t).start();
for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}
A. compile error
B. prints: yes yes yes yes yes yes and so on
C. prints: no no no no no no no no and so on
D. prints: yes no yes no ye no ye no and so on
E. the output cannot be determinated
我的答案是C
标答是E
解释如下:
24. E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.
没看明白,难道线程不是独立于平台的吗?谁能详细解释一下?
...全文
相关推荐
八皇后问题演示程序-源代码及编译结果
Delphi编写的八皇后演示程序代码1 演示了Grid组件的自绘事件的施用2 多线程进程的中断与继续3 八皇后问题的解决办法
C++封装的一个跨平台的线程类和锁类
C++封装的一个linux和windows的线程类,一个线程锁类,两个配合使用实现一份多线程的代码适应两个平台
基于python+gurobi的数值双层规划问题求解
该资源采用python编写,通过调用gurobi对数值双层优化问题进行求解,是学习双层规划的绝佳材料。
Python自定义线程类简单示例
主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下
WTL的线程类
展示一小组ATL / WTL类,这些类简化了线程的使用。


