SCJP題庫第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. Ans: E 解說 : E是說若宣告doThings方法為靜態的方法將使得該類別為"執行緒安全"。 thread-safe是說不管執行緒如何執行,都不會破壞掉類別資料完整性。 若方法是物件的成員,則多個執行緒就可以執行多個版本的 doThings方法,好像多人一起操作一個共同的整數資料x(因為x是靜態的/類別的,只有一...