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?

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.