QUESTION NO: 82
Which three will compile and run without exception? (Choose three.)
A. private synchronized Object o;
ActualTests.com
B. void go() {
synchronized() { /* code here */ }
C. public synchronized void go() { /* code here */ }
D. private synchronized(this) void go() { /* code here */ }
E. void go() {
synchronized(Object.class) { /* code here */ }
F. void go() {
Object o = new Object();
synchronized(o) { /* code here */ }
Answer: C,E,F
解析:
对于用synchronized 修饰的可以有两种,同步方法和同步代码块,若是同步方法,则直接在方法名前加上synchronized 即可,此时的同步监视器为this;若为同步代码块,则应有一个同步监视器,格式为synchronized (Object o)。
Which three will compile and run without exception? (Choose three.)
A. private synchronized Object o;
ActualTests.com
B. void go() {
synchronized() { /* code here */ }
C. public synchronized void go() { /* code here */ }
D. private synchronized(this) void go() { /* code here */ }
E. void go() {
synchronized(Object.class) { /* code here */ }
F. void go() {
Object o = new Object();
synchronized(o) { /* code here */ }
Answer: C,E,F
解析:
对于用synchronized 修饰的可以有两种,同步方法和同步代码块,若是同步方法,则直接在方法名前加上synchronized 即可,此时的同步监视器为this;若为同步代码块,则应有一个同步监视器,格式为synchronized (Object o)。