Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class
thread-safe.
该选哪个,为什么?thx。
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class
thread-safe.
该选哪个,为什么?thx。
解决方案 »
- 运行一个关于ArrayList的例子,编译出错,不知是什么原因,请赐教。
- 哪位高人有好办法能算出当前CPU最大支持工作线程数?????
- filechooser获取路径时出错
- JTextArea中的select问题??
- 模拟银行 ( 求助)
- 程序启动时SPLASH WINDOW的问题
- 一个TextListener的问题????
- 有看过《java core》那本书的朋友进来帮我一下,小弟先谢过了。
- 如何创建文件夹,我只能创建文件
- 初学java,请问各位如何将Edit的内容赋给一个Double变量??急急!!
- java连接MYSQL的问题,各位大牛们看看吧,小弟我疯了、、、
- 一道智力题,特急!!明天就得去面试了!
Error: Main method not found in class TestSeven, please define the main method as:
public static void main(String[] args)
B.运行期不会有错
C.同步run只能使run方法线程安全,无法使类线程安全
D.x因为是私有,而且只暴露在同步方法中,因此安全
E.这个同意,操作此类数据的入口全在doThings方法了
F.尚不能使doThings方法安全,每次都是一把新锁,遑论class thread-safe了
A。错误,能编译
B。运行时异常,也不对
C.同步对象的方法不能保证静态属性的线程安全,错
D.没怎么看懂,x不是安全的,错误
E.doThings这个如果定义为静态的,能保证static的属性是线程安全的,正确
F.明显不对,在synchronized中使用new
[Quote=引用 3 楼 dracularking 的回复:]
D.x因为是私有,而且只暴露在同步方法中,因此安全