Java Reference
In-Depth Information
Whenyourunthisapplication,youwillobserveashuffledsequenceofintegersthat
is similar to the following sequence that I observed:
7 1 5 2 9 8 6 4 3 0
EXERCISES
The following exercises are designed to test your understanding of the concurrency
utilities,
Objects
, and
Random
:
1. The Java documentation for the
Semaphore
class presents a
Pool
class
that demonstrates how a semaphore can control access to a pool of items.
Because
Pool
is incomplete, introduce a single resource (replace
pro-
tected Object[] items = ...
with an array containing this re-
source in its single entry) and then demonstrate
Pool
's
getItem()
and
putItem()
methodsinthecontextofapairofthreadslaunchedfromthe
main()
method of a
SemaphoreDemo
class.
2. Create an
EqualsDemo
application to play with
Objects
'
deep-
Equals()
method.Aswellasan
EqualsDemo
class,thisapplicationde-
clares
Car
and
Wheel
classes. A
Car
instance contains (typically four)
Wheel
instances, and a
Wheel
instance contains a brand name. Each of
Car
and
Wheel
must override
Object
's
equals()
method but does
nothavetooverride
hashCode()
inthisexample.Your
main()
method
shouldcontainthefollowingcodeandgeneratetheoutputshowninthecom-
ments:
Car[] cars1 = { new Car(4, "Goodyear"), new Car(4,
"Goodyear") };
Car[] cars2 = { new Car(4, "Goodyear"), new Car(4,
"Goodyear") };
Car[] cars3 = { new Car(4, "Michelin"), new Car(4,
"Goodyear") };
Car[] cars4 = { new Car(3, "Goodyear"), new Car(4,
"Goodyear") };
Car[] cars5 = { new Car(4, "Goodyear"), new Car(4,
"Goodyear"),
new Car(3, "Michelin") };
System.out.println(Objects.deepEquals(cars1,
cars2)); // Output: true
System.out.println(Objects.deepEquals(cars1,