December 1999
Intermediate to advanced
816 pages
20h 27m
English
When there are multiple Threads running in a JVM, it is possible that multiple Threads will want to modify the same resource (object) at the same time. The simplest way to illustrate this is with the classic producer-consumer model. Imagine an array of float primitives:
float[] fltArray = new float[6];
This creates an array for holding six float primitives. In the producer-consumer model, a Thread, t1, tries to add floats to this array as fast as it can. Another Thread, t2, the consumer, tries to read and remove those floats as fast as it can. Typically, some kind of counter would be used to track which slot in the array last had a float put in it or taken from it. Listing 11.3 shows an unsynchronized (dangerous) way ...
Read now
Unlock full access