24.1 | Why do you need multithreading? How can multiple threads run simultaneously in a single-processor system? |
24.2 | How do you declare a task class? How do you create a thread for a task? |
24.3 | What would happen if you replace the start() method by the run() method in lines 14 “16 in Listing 24.1?
|
| |
24.4 | What is wrong in the following two programs? Correct the errors. |
24.5 | Which of the following methods are instance methods in java.lang.Thread ? Which method may throw an InterruptedException ? Which of them are deprecated in Java 2? run , start , stop , suspend , resume , sleep , interrupt , yield , join |
24.6 | If a loop contains a method that throws an InterruptedException , why should the loop be placed inside a try-catch block? |
24.7 | How do you set a priority for a thread? What is the default priority? |
24.8 | When should you use a timer or a thread to control animation? What are the advantages and disadvantages of using a thread and a timer? |
24.9 | What is the event dispatcher thread? How do you let a task run from the event dispatcher thread? |
24.10 | What are the benefits of using a thread pool? |
24.11 | How do you create a thread pool with three fixed threads? How do you submit a task to the thread pool? How do you know that all the tasks are finished? |
24.12 | Give some examples of possible resource corruption when running multiple threads. How do you synchronize conflicting threads? |
24.13 | Suppose you place the statement in line 26 of Listing 24.7, AccountWithoutSync.java, inside a synchronized block to avoid race conditions, as follows : synchronized ( this ) { account.deposit( 1 ); } Does it work? |
24.14 | How do you create a lock object? How do you acquire a lock and release a lock? |
24.15 | How do you create a condition on a lock? What are the await() , signal() , and signalAll() methods for? |
| |
24.16 | What would happen if the while loop in lines 58 “59 of Listing 24.9, ThreadCooperation.java, is changed to an if statement?
|
24.17 | Why does the following class have a syntax error? 1 import javax.swing.*; 2 3 public class Test extends JApplet implements Runnable { 4 public void init() throws InterruptedException { 5 Thread thread = new Thread( this ); 6 thread.sleep( 1000 ); 7 } 8 9 public synchronized void run() { 10 } 11 } |
24.18 | What is the possible cause for IllegalMonitorStateException ? |
24.19 | Can wait() , notify() , and notifyAll() be invoked from any object? What is the purpose of these methods? |
24.20 | What is wrong in the following code? synchronized (object1) { try { while (!condition) object2.wait(); } catch (InterruptedException ex) { } } |
24.21 | What blocking queues are supported in JDK 1.5? |
24.22 | What method do you use to add an element to an ArrayBlockingQueue ? What happens if the queue is full? |
24.23 | What method do you use to retrieve an element from an ArrayBlockingQueue ? What happens if the queue is empty? |
24.24 | What are the similarities and differences between a lock and a semaphore? |
24.25 | How do you create a semaphore that allows three concurrent threads? How do you acquire a semaphore? How do you release a semaphore? |
24.26 | What is deadlock? How can you avoid deadlock? |
24.27 | What is a synchronized collection? Is ArrayList synchronized? How do you make it synchronized? |
24.28 | Explain why an iterator is fail-fast. |
24.29 | For a JProgressBar , what is the property that displays the percentage of work completed? How do you set its orientation? |