Exercises

  1. Give three examples of when threads might be used to an advantage in a program. Describe a circumstance when it would not be advantageous to use threads.

  2. What are the two ways of creating a new thread in Java?

  3. Take your favorite sorting algorithm and make it multithreaded. Hint: A recursive partitioning algorithm like quicksort is the best candidate for this. Quicksort simply divides the array to be sorted into two pieces, then moves numbers about until all the numbers in one piece are smaller (or at least no larger) than all the numbers in the other piece. Repeat the algorithm on each of the pieces. When the pieces consist of just one element, the array is sorted.

  4. What resources are consumed by each individual thread?

  5. Refer back to the text giving the information about the synchronization bug in the thread library. What code change would you recommend to fix that bug and why? Look at the source code for Vector in the current run-time library. Did they follow your recommendation?

  6. Rewrite the code example in the previous chapter that shows how to test a range of prime numbers for primes. Give the argument range to the threads by using thread local storage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.89.2