Given:
foo and bar are public references available to many other threads, foo refers to a Thread and bar is an Object.
The thread foo is currently executing bar.wait().
From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?
A. foo.notify();
B. bar.notify();
C. foo.notifyAll();
D. Thread.notify();
E. bar.notifyAll();
F. Object.notify();
答案:E
參考:13-5 執行緒的互動處理
------------------------------------------------------
foo and bar是二個物件,而foo 是一個Thread 的物件,而bar 為該bar Thread 要存取的物件
目前 thread foo正在執行bar.wait(),
另一條thread 如要喚醒的話,有何方式保證foo will stop executing wait()
bar.notifyAll();喚醒正在等待bar物作的所有thread
bar.notify();隨機喚醒正在等待bar物作的一條thread
2009年2月26日 星期四
第235題
Given:
11. public class PingPong implements Runnable{
12. synchronized void hit(long n){
13. for(int i=1; i<3; color="#ffffff">CD
參考:13-4 執行緒的同步性與安全性
-----------------------------------------------
解析:雖然第12行的hit()方法加上synchronized ,但第17,18行各自啟動1條Thread
並在第21行透過不同的pingping物作呼叫第12行hit()方法,所以同一時間這2 條Thread
都可以執行,因為synchronized 無法鎖定不同物件的Thread;因此輸出時會看到2條Thread
(例如Thread id 分別為7和8)交互執行,並各自遞增i的值,而不會影響對方
11. public class PingPong implements Runnable{
12. synchronized void hit(long n){
13. for(int i=1; i<3; color="#ffffff">CD
參考:13-4 執行緒的同步性與安全性
-----------------------------------------------
解析:雖然第12行的hit()方法加上synchronized ,但第17,18行各自啟動1條Thread
並在第21行透過不同的pingping物作呼叫第12行hit()方法,所以同一時間這2 條Thread
都可以執行,因為synchronized 無法鎖定不同物件的Thread;因此輸出時會看到2條Thread
(例如Thread id 分別為7和8)交互執行,並各自遞增i的值,而不會影響對方
第233題
Given:
1. public class Threads1{
2. int x = 0;
3. public class Runner implements Runnable{
4. public void run(){
S. int current = 0;
6. for(int i=0; i<4; i++){
7. current = x;
8. System.out.print(current + ", ");
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args){
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable rl = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23. }
Which two are possible results? (Choose two.)
A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
答案:AC
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
1. public class Threads1{
2. int x = 0;
3. public class Runner implements Runnable{
4. public void run(){
S. int current = 0;
6. for(int i=0; i<4; i++){
7. current = x;
8. System.out.print(current + ", ");
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args){
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable rl = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23. }
Which two are possible results? (Choose two.)
A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
答案:AC
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
第236題
Given:
1. class Computation extends Thread{
2.
3. private int num;
4. private boolean isComplete;
5. private int result;
6.
7. public Computation(int num){this.num = num;}
8.
9. public synchronized void run(){
10. result = num * 2;
11. isComplete = true;
12. notify();
13. }
14.
15. public synchronized int getResult(){
16. while(!isComplete){
17. try{
18. wait();
19. }catch(InterruptedException e){}
20. }
21. return result;
22. }
23.
24. public static void main(String[] args){
25. Computation[] computations = new Computation[4];
26. for(int i=0; i27. computations[i] = new Computation(i);
28. computations[i].start();
29. }
30. for(Computation c : computations)
31. System.out.print(c.getResult() + " ");
32. }
33. }
What is the result?
A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output "0 6".
E. The code may run with output "2 0 6 4".
F. The code may run with output "0 2 4 6".
答案:F
參考:13-4 執行緒的同步性與安全性
1. class Computation extends Thread{
2.
3. private int num;
4. private boolean isComplete;
5. private int result;
6.
7. public Computation(int num){this.num = num;}
8.
9. public synchronized void run(){
10. result = num * 2;
11. isComplete = true;
12. notify();
13. }
14.
15. public synchronized int getResult(){
16. while(!isComplete){
17. try{
18. wait();
19. }catch(InterruptedException e){}
20. }
21. return result;
22. }
23.
24. public static void main(String[] args){
25. Computation[] computations = new Computation[4];
26. for(int i=0; i
28. computations[i].start();
29. }
30. for(Computation c : computations)
31. System.out.print(c.getResult() + " ");
32. }
33. }
What is the result?
A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output "0 6".
E. The code may run with output "2 0 6 4".
F. The code may run with output "0 2 4 6".
答案:F
參考:13-4 執行緒的同步性與安全性
第232題
Given:
1. public class Threads5{
2. public static void main(String[] args){
3. new Thread(new Runnable(){
4. public void run(){
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "bar".
D. The code executes normally, but nothing prints.
答案:C
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
------------------------------------------
執行到start()時,會跑public void run(){
1. public class Threads5{
2. public static void main(String[] args){
3. new Thread(new Runnable(){
4. public void run(){
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "bar".
D. The code executes normally, but nothing prints.
答案:C
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
------------------------------------------
執行到start()時,會跑public void run(){
第238題
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread(){
public void run(){doStuff();}
};
B. new Thread(){
public void start(){doStuff();}
};
C new Thread(){
public void start(){doStuff();}
}.run();
D. new Thread(){
public void run(){doStuff();}
}.start();
E. new Thread(new Runnable(){
public void run(){doStuff();}
}).run();
F. new Thread(new Runnable(){
public void run(){doStuff();}
}).start();
答案:DF
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
A. new Thread(){
public void run(){doStuff();}
};
B. new Thread(){
public void start(){doStuff();}
};
C new Thread(){
public void start(){doStuff();}
}.run();
D. new Thread(){
public void run(){doStuff();}
}.start();
E. new Thread(new Runnable(){
public void run(){doStuff();}
}).run();
F. new Thread(new Runnable(){
public void run(){doStuff();}
}).start();
答案:DF
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
第231題
Given:
11. Runnable r = new Runnable(){
12. public void run(){
13. System.out.print("Cat");
14. }
15. };
16. Thread t = new Thread(r){
17. public void run(){
18. System.outprint("Dog");
19. }
20. };
21. t.start();
What is the result?
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
答案:B
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
--------------------------------
17行改寫了12行.
11. Runnable r = new Runnable(){
12. public void run(){
13. System.out.print("Cat");
14. }
15. };
16. Thread t = new Thread(r){
17. public void run(){
18. System.outprint("Dog");
19. }
20. };
21. t.start();
What is the result?
A. Cat
B. Dog
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
答案:B
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
--------------------------------
17行改寫了12行.
第230題
Given that t1 is a reference to a live thread, which is true?
A. The Thread.sleep() method can take t1 as an argument.
B. The Object.notify() method can take t1 as an argument.
C. The Thread.yield() method can take t1 as an argument.
D. The Thread.setPriority() method can take t1 as an argument.
E. The Object.notify() method arbitrarily chooses which thread to notify.
答案:E
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面、13-5 執行緒的互動處理
-------------------------------------
E. notify() 任意喚醒一條執行緒.
A~D 的方法都不是拿t1作為參數.
A. The Thread.sleep() method can take t1 as an argument.
B. The Object.notify() method can take t1 as an argument.
C. The Thread.yield() method can take t1 as an argument.
D. The Thread.setPriority() method can take t1 as an argument.
E. The Object.notify() method arbitrarily chooses which thread to notify.
答案:E
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面、13-5 執行緒的互動處理
-------------------------------------
E. notify() 任意喚醒一條執行緒.
A~D 的方法都不是拿t1作為參數.
第239題
Given:
1. public class TestOne{
2. public static void main(String[] args) throws Exception{
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "sleep".
D. The code executes normally, but nothing is printed.
答案:C
參考:13-2 Java執行緒與Thread類別
1. public class TestOne{
2. public static void main(String[] args) throws Exception{
3. Thread.sleep(3000);
4. System.out.println("sleep");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "sleep".
D. The code executes normally, but nothing is printed.
答案:C
參考:13-2 Java執行緒與Thread類別
第241題
Given:
1. public class Threads3 implements Runnable{
2. public void run(){
3. System.out.print("running");
4. }
5. public static void main(String[] args){
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".
答案:E
參考:13-3 Runnable介面
1. public class Threads3 implements Runnable{
2. public void run(){
3. System.out.print("running");
4. }
5. public static void main(String[] args){
6. Thread t = new Thread(new Threads3());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "running".
D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".
答案:E
參考:13-3 Runnable介面
第242題
Given:
public class NamedCounter{
private final String name;
private int count;
public NamedCounter(String name){this.name = name;}
public String getName(){return name;}
public void increment(){count++;}
public int getCount(){return count;}
public void reset(){count = 0;}
Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose
three.)
A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword
答案:ACE
參考:13-3 Runnable介面
public class NamedCounter{
private final String name;
private int count;
public NamedCounter(String name){this.name = name;}
public String getName(){return name;}
public void increment(){count++;}
public int getCount(){return count;}
public void reset(){count = 0;}
Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose
three.)
A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword
答案:ACE
參考:13-3 Runnable介面
第243題
Given that Triangle implements Runnable, and:
31. void go() throws Exception{
32. Thread t = new Thread(new Triangle());
33. t.start();
34. for(int x=1; x<100000; x++){
35. //insert code here
36. if(x%100 == 0) System.out.print("g");
37. }}
38. public void run(){
39. try{
40. for(int x=1; x<100000; x++){
41. //insert the same code here
42. if(x%100 == 0) System.out.print("t");
43. }
44. }catch(Exception e){}
45. }
Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to
temporarily pause and allow the other thread to execute? (Choose two.)
A. Thread.wait();
B. Thread.join();
C. Thread.yield();
D. Thread.sleep(1);
E. Thread.notify();
答案:CD
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面
31. void go() throws Exception{
32. Thread t = new Thread(new Triangle());
33. t.start();
34. for(int x=1; x<100000; x++){
35. //insert code here
36. if(x%100 == 0) System.out.print("g");
37. }}
38. public void run(){
39. try{
40. for(int x=1; x<100000; x++){
41. //insert the same code here
42. if(x%100 == 0) System.out.print("t");
43. }
44. }catch(Exception e){}
45. }
Which two statements, inserted independently at both lines 35 and 41, tend to allow both threads to
temporarily pause and allow the other thread to execute? (Choose two.)
A. Thread.wait();
B. Thread.join();
C. Thread.yield();
D. Thread.sleep(1);
E. Thread.notify();
答案:CD
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面
第244題
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.
答案:E
參考:13-4 執行緒的同步性與安全性
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.
答案:E
參考:13-4 執行緒的同步性與安全性
第229題
Given:
1. public class TestFive{
2. private int x;
3. public void foo(){
4. int current = x;
5. x = current + 1;
6. }
7. public void go(){
8. for(int i=0; i<5; i++){
9. new Thread(){
10. public void run(){
11. foo();
12. System.out.print(x + ", ");
13. }}.start();
14. }}
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A. move the line 12 print statement into the foo() method
B. change line 7 to public synchronized void go(){
C. change the variable declaration on line 2 to private volatile int x;
D. wrap the code inside the foo() method with a synchronized(this) block
E. wrap the for loop code inside the go() method with a synchronized block synchronized(this){//for loop code here}
答案:AD
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
---------------------------------------
以題意來看同依時間只能有一個執行續執行.
A. 在原處的話,同時間有5條執行緒.
D. 在foo() 裡面使用 synchronize 區塊,這樣同時間就只能有一條執行緒.
1. public class TestFive{
2. private int x;
3. public void foo(){
4. int current = x;
5. x = current + 1;
6. }
7. public void go(){
8. for(int i=0; i<5; i++){
9. new Thread(){
10. public void run(){
11. foo();
12. System.out.print(x + ", ");
13. }}.start();
14. }}
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.)
A. move the line 12 print statement into the foo() method
B. change line 7 to public synchronized void go(){
C. change the variable declaration on line 2 to private volatile int x;
D. wrap the code inside the foo() method with a synchronized(this) block
E. wrap the for loop code inside the go() method with a synchronized block synchronized(this){//for loop code here}
答案:AD
參考:8-5-4 匿名內部類別、13-2 Java執行緒與Thread類別、13-3 Runnable介面
---------------------------------------
以題意來看同依時間只能有一個執行續執行.
A. 在原處的話,同時間有5條執行緒.
D. 在foo() 裡面使用 synchronize 區塊,這樣同時間就只能有一條執行緒.
第228題
Which three will compile and run without exception? (Choose three.)
A. private synchronized Object o;
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 */}
答案:CEF
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面、13-5 執行緒的互動處理
-------------------------------------
E. 使用synchronize 區塊 鎖定類別.
F. 使用synchronize 區塊 鎖定物件.
A. private synchronized Object o;
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 */}
答案:CEF
參考:13-2 Java執行緒與Thread類別、13-3 Runnable介面、13-5 執行緒的互動處理
-------------------------------------
E. 使用synchronize 區塊 鎖定類別.
F. 使用synchronize 區塊 鎖定物件.
第219題
Given:
1. public class Threads2 implements Runnable{
2.
3. public void run(){
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args){
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
答案:DE
參考:13-2 Java執行緒與Thread類別
1. public class Threads2 implements Runnable{
2.
3. public void run(){
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args){
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem
B. run.
java.lang.RuntimeException: Problem
C. End of method.
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.
答案:DE
參考:13-2 Java執行緒與Thread類別
第220題
Which factor or factors
A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify() and notifyAll() methods are used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by
inserting invocations of Thread.yield().
答案:AF
參考:13-5 執行緒的互動處理
--------------------------------------------------------------
會有多條threads 進入deadlock
A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify() and notifyAll() methods are used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by
inserting invocations of Thread.yield().
答案:AF
參考:13-5 執行緒的互動處理
--------------------------------------------------------------
會有多條threads 進入deadlock
訂閱:
文章 (Atom)