2.9 Problems

 < Day Day Up > 



2.9 Problems

  1. Is it easier or more difficult to test and debug a concurrent program than a procedural program? Why?

  2. Throw an exception in the run method for a program where the run method is called directly from the main. Throw an exception from the run method that is called by using the start method in a thread. What is the difference between the two programs? What does this say about the program stack for the two types of executions? How do you think your answer to this question will impact the programs you write that use threads?

  3. Can a race condition ever occur when an automatic (or local) variable is shared between two threads? Why or why not? Consider where in memory automatic variables are stored. Assume the variable is declared and allocated in a single method.

  4. What are all the possible program outputs if Exhibit 6 (Program2.5) is run with three threads instead of two? How many outputs are possible with four threads? With five threads? What if three lines are output for each thread instead of two? What does this say about the complexity of predicting the output of concurrent programs with multiple threads?

  5. Make the Exhibit 16 (Program2.6) safe by creating separate, non-shared resources to do the swap. Show two ways to accomplish this.

  6. Exhibit 17 (Program2.6) showed the existence of a race condition, and Exhibit 20 (Program2.7b) showed a solution to the problem. Run Exhibit 20 (Program2.7b) 100 times. Is the answer always correct? Because Exhibit 17 (Program2.6) failed occasionally when run, and Program2.7b did not, can we say that testing showed that Exhibit 20 (Program2.7b) is correct? Why or why not?

  7. What happens if a call to wait occurs in the middle of a synchronized block? Are any variables that are accessed still safe, or can race conditions occur? Why?

  8. Why do you think that Java did not specify notify/notifyAll to implement a fair (i.e., first-in, first-out, or FIFO) mechanism for notifying waiting objects? Could a fair mechanism have been used? If so, do you think it was a good decision not to implement a fair mechanism? Explain. Consider the behavior of thread priorities in your answer.

  9. Do synchronized methods lock on all methods within an object, on one method across all objects for that class, or on all methods across all objects? Explain why or why not.

  10. Can static methods be synchronized? If so, how? If not, why not?

  11. Can Java synchronize on any block of code or only methods? Can you justify why this decision was made?

  12. If no object is specified, what object is used in a Java synchronized statement? If synchronization can be specified, how is it done? Can method synchronization (using the synchronize statement to modify the method declaration) specify the object to use?

  13. The binary semaphore given in this chapter is a special case of a counting semaphore. In a counting semaphore, some positive numbers of resources are available. The counting semaphore has two methods: getResource and releaseResource. Each time a getResource call is made, the number of resources available is decremented by 1. As long as the number of resources is greater than or equal to () zero, the thread requesting the resource can continue. If, however, the number of resources is less than 0, the thread requesting the resource must wait for a releaseResource call to occur, which increments the number of resources available. Implement a counting semaphore with a constructor that takes the initial number of resources available and the getResource and releaseResource methods. In what situations is a counting semaphore useful?

  14. Implement a binary semaphore using the counting semaphore from Problem 13.

  15. Implement the counting semaphore without a constructor that sets the initial number of resources available. How can you set the initial number of resources without setting it in the constructor?

  16. Compare Java threads and tasks in Ada. What are the advantages and disadvantages of both?

  17. Compare the locking strategies of Ada-protected types with synchronized blocks in Java. What are the advantages and disadvantages?

  18. Compare threads and semaphores using Pthreads in C/C++ and Java synchronized blocks and threads. What are the advantages and disadvantages of each implementation?

  19. What happens if a call to notify or notifyAll is made, and no threads are currently waiting? Why?

  20. When a program calls Thread.sleep, is it guaranteed only to sleep for the length of time specified in the call? Can the sleep time be longer? Can the sleep time be shorter? Explain your answer.

  21. Assume that the notifyAll call is fair in the sense that the first thread to wait on the object is the first one moved to the ready set. Is this thread guaranteed to get the object lock when notifyAll is called with more than one thread waiting? Why or why not? Consider thread priorities when answering the question.

  22. Assume priorities are used in a program, and multiple threads are waiting on the same object. When a notify call is made, will the thread with the highest priority be the one that gets notified? Justify this decision by considering the Big-O of implementing a notify that chooses any thread vs. one that chooses the highest priority thread. Note: Also consider the interaction between the notify and the ready set.

  23. Which of the following actions results in an object lock being dropped?

    • Call to notify

    • Call to notifyAll

    • Call to yield

    • Call to wait

    • Call to Thread.sleep

    • Leaving a synchronized block

  24. A common programming mistake is to make a notify or wait call when you are not in a synchronized block. What error is produced if you try to do a notify or a wait on an object when you are not in a synchronized block? Is it a runtime or compile time error? Explain the error.

  25. Is the TurnPrinter object in Exhibit 27 (Program2.13) a component by the definition in Chapter 1? Is the BinarySemaphore object? Why or why not?

  26. How does the use of priorities for threads impact which thread will be executed when several are moved from an object's wait set to the ready set? Is Java effective when threads of differing priorities are used?

  27. Explain why deadlock is common in database management systems. Do you think deadlock can be predicted and thus avoided? If not, how do you think database management systems handle deadlock?

  28. What can be inferred about a program when no threads are in the ready or running state? If all of the threads are in wait states? If some threads are in the sleeping or blocked state?

  29. How many ways are there to cause a thread to drop a synchronized lock?

  30. For the program in Exhibit 28, consider the statements commented out of the swap method. Which, if any, of the statements if uncommented could result in a race condition? Why?

    Exhibit 28: SolveRaceConditon_2.java

    start example

     class SwapInt {   private int tmp;   synchronized public void swap(SolveRaceCondition_2 s) {   tmp = s.val1;   s.val1 = s.val2;     try {       //Thread.yield();       //Thread.sleep(1000);       //Thread.notify();       //Thread.notifyAll();       //wait();       //wait(1000);       //wait(1);       //wait(0);     } catch (Exception e) {       e.printStackTrace();     }   s.val2 = tmp;   } } public class SolveRaceCondition_2 implements Runnable {   int val1, val2;   static SwapInt so = new SwapInt();   public SolveRaceCondition_2(int i1, int i2){val1 = i1; val2 = i2; }   public void run() {     so.swap(this);   System.out.println("Val1 = "+ val1 + "Val2 = "+ val2);   }   public static void main(String args[]) {     (new Thread(new SolveRaceCondition_2(4,7))).start();     (new Thread(new SolveRaceCondition_2(2,5))).start();   }     } 

    end example

  31. Consider the following program (Exhibit 29) that uses threads (threads are run one after the other). By the definition in Chapter 1, is this program concurrent? Why or why not?

    Exhibit 29: Problem2_31

    start example

     public class Problem2_31 implements Runnable {      public void run() {           System.out.println("Starting run");           try {                Thread.sleep((int)(1000 * Math.random()));           } catch (InterruptedException ie) {           }      }      public static void main(String args[]) {           Thread t1 = new Thread(new Problem2_31());           Thread t2 = new Thread(new Problem2_31());           Thread t3 = new Thread(new Problem2_31());           try {                t1.start();                t1.join();                t2.start();                t2.join();                t3.start();                t3.join();           } catch (InterruptedException ie) {           }      } } 

    end example

  32. Consider the following program (Exhibit 30) that uses threads (threads do not synchronize). By the definition in Chapter 1, is this program concurrent? Why or why not?,

    Exhibit 30: Problem2_32

    start example

     public class Problem2_32 implements Runnable {      public void run() {           System.out.println("Starting run");           try {                Thread.sleep((int)(1000 * Math.random()));           } catch (InterruptedException ie) {           }      }      public static void main(String args[]) {           Thread t1 = new Thread(new Problem2_32());           Thread t2 = new Thread(new Problem2_32());           Thread t3 = new Thread(new Problem2_32());           try {                t1.start();                t2.start();                t3.start();           } catch (InterruptedException ie) {           }      } } 

    end example



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net