Java Reference
In-Depth Information
30.2
( Racing cars ) Rewrite Programming Exercise 15.29 using a thread to control
car racing. Compare the program with Programming Exercise 15.29 by setting
the delay time to 10 in both programs. Which one runs the animation faster?
30.3
( Raise flags ) Rewrite Listing 15.13 using a thread to animate a flag being
raised. Compare the program with Listing 15.13 by setting the delay time to 10
in both programs. Which one runs the animation faster?
Sections 30.8-30.12
30.4
( Synchronize threads ) Write a program that launches 1,000 threads. Each
thread adds 1 to a variable sum that initially is 0 . Define an Integer wrapper
object to hold sum . Run the program with and without synchronization to see
its effect.
30.5
( Display a running fan ) Rewrite Programming Exercise 15.28 using a thread to
control the fan animation.
30.6
( Bouncing balls ) Rewrite Listing 15.17 BallPane.java using a thread to animate
bouncing ball movements.
30.7
( Control a clock ) Rewrite Programming Exercise 15.32 using a thread to con-
trol the clock animation.
30.8
( Account synchronization ) Rewrite Listing 30.6, ThreadCooperation.java,
using the object's wait() and notifyAll() methods.
30.9
( Demonstrate ConcurrentModificationException ) The iterator is fail-
fast . Write a program to demonstrate it by creating two threads that concurrently
access and modify a set. The first thread creates a hash set filled with numbers,
and adds a new number to the set every second. The second thread obtains an
iterator for the set and traverses the set back and forth through the iterator every
second. You will receive a ConcurrentModificationException because
the underlying set is being modified in the first thread while the set in the sec-
ond thread is being traversed.
*30.10
( Use synchronized sets ) Using synchronization, correct the problem
in the preceding exercise so that the second thread does not throw a
ConcurrentModificationException .
Section 30.15
*30.11
( Demonstrate deadlock ) Write a program that demonstrates deadlock.
Section 30.18
*30.12
( Parallel array initializer ) Implement the following method using the Fork/
Join Framework to assign random values to the list.
public static void parallelAssignValues( double [] list)
Write a test program that creates a list with 9,000,000 elements and invokes
parallelAssignValues to assign random values to the list. Also
implement a sequential algorithm and compare the execution time of the
two. Note that if you use Math.random() , your parallel code execu-
tion time will be worse than the sequential code execution time because
Math.random() is synchronized and cannot be executed in parallel. To
fix this problem, create a Random object for assigning random values to a
small list.
 
Search WWH ::




Custom Search