当前位置: 技术问答>java相关
scjp的问题2道
来源: 互联网 发布时间:2015-07-22
本文导语: (1) 1. public class X implements Runnable( 2. private int x; 3. private int y; 4. 5. public static void main(String[]args) 6. X that = new X(); 7. (new Thread(that)).start(); 8. (new Thread(that)).start(); 9. ) 10. 11. public ...
(1)
1. public class X implements Runnable(
2. private int x;
3. private int y;
4.
5. public static void main(String[]args)
6. X that = new X();
7. (new Thread(that)).start();
8. (new Thread(that)).start();
9. )
10.
11. public void run() (
12. for (; (
13. x++;
14. y++;
15. System.out.printIn(“x=” + x + “, y = ” + y);
16. )
17. )
18. )
What is the result?
A. Errors at lines 7 and 8 cause compilation to fail.
B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”).
C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”).
D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”).
(2)Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) (x=1;)
5. public synchronized void setY (int i) (y=1;)
6. public synchronized void setXY(int 1)(set X(i); setY(i);)
7. public synchronized Boolean check() (return x !=y;)
8. )
Under which conditions will check () return true when called from a different class?
A. check() can never return true
B. check() can return true when setXY is called by multiple threads
C. check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
请解释一下synchronized ?谢谢。
1. public class X implements Runnable(
2. private int x;
3. private int y;
4.
5. public static void main(String[]args)
6. X that = new X();
7. (new Thread(that)).start();
8. (new Thread(that)).start();
9. )
10.
11. public void run() (
12. for (; (
13. x++;
14. y++;
15. System.out.printIn(“x=” + x + “, y = ” + y);
16. )
17. )
18. )
What is the result?
A. Errors at lines 7 and 8 cause compilation to fail.
B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”).
C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”).
D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”).
(2)Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) (x=1;)
5. public synchronized void setY (int i) (y=1;)
6. public synchronized void setXY(int 1)(set X(i); setY(i);)
7. public synchronized Boolean check() (return x !=y;)
8. )
Under which conditions will check () return true when called from a different class?
A. check() can never return true
B. check() can return true when setXY is called by multiple threads
C. check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
请解释一下synchronized ?谢谢。
|
(1)第一题我的答案是D
因为7、8句同时设置了线程的开始,但是有一个先后的次序。
(2)synchronized是将此方法设置为同步,即同一时刻只能由一个线程能够访问这个方法。这题我的答案是A和D。
不知道你的答案为何?大家交流一下。
因为7、8句同时设置了线程的开始,但是有一个先后的次序。
(2)synchronized是将此方法设置为同步,即同一时刻只能由一个线程能够访问这个方法。这题我的答案是A和D。
不知道你的答案为何?大家交流一下。
|
A1 = B
因为在多线程中,没有加锁的方法可以被打断。所以 x++; y++; 的当中可以被打断。因为有两个线程再运动,所以可能 T1 [ x++ ] 后是 T2 [ x++; y++; println(); ] 结果就会 x 比 y 大一了。
A2 = C
同意iamcyh(蓝色虾)对synchronized 的解释。但是当method 中只有一个操作的时候,如 public synchronized void setX (int i) (x=1;) 与
public void setX (int i) (x=1;) 是一样的。所以当分别给 setX(), setY() 时会有 x!=y的现象。
因为在多线程中,没有加锁的方法可以被打断。所以 x++; y++; 的当中可以被打断。因为有两个线程再运动,所以可能 T1 [ x++ ] 后是 T2 [ x++; y++; println(); ] 结果就会 x 比 y 大一了。
A2 = C
同意iamcyh(蓝色虾)对synchronized 的解释。但是当method 中只有一个操作的时候,如 public synchronized void setX (int i) (x=1;) 与
public void setX (int i) (x=1;) 是一样的。所以当分别给 setX(), setY() 时会有 x!=y的现象。
|
1 d
2 a d
2 a d