|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
All Java programs are compiled into class files that contain bytecodes, the machine language of the Java virtual machine. In this JavaWorld classic, Bill Venners goes under the hood of the JVM to explain the mechanics of thread synchronization, including shared data, locks, monitors, and synchronized. Like many JW Archives, this article is as relevant for students of JVM internals today as it was when it was first published in July 1997.
This month's Under the Hood looks at thread synchronization in both the Java language and the Java virtual machine (JVM). This article is the last in the long series of bytecode articles I began last summer. It describes the only two opcodes directly related to thread synchronization, the opcodes used for entering and exiting monitors.
One of the strengths of the Java programming language is its support for multithreading at the language level. Much of this support centers on coordinating access to data shared among multiple threads.
The JVM organizes the data of a running Java application into several runtime data areas: one or more Java stacks, a heap, and a method area. For a backgrounder on these memory areas, see the first Under the Hood article: "The lean, mean virtual machine."
Inside the Java virtual machine, each thread is awarded a Java stack, which contains data no other thread can access, including the local variables, parameters, and return values of each method the thread has invoked. The data on the stack is limited to primitive types and object references. In the JVM, it is not possible to place the image of an actual object on the stack. All objects reside on the heap.
There is only one heap inside the JVM, and all threads share it. The heap contains nothing but objects. There is no way to place a solitary primitive type or object reference on the heap -- these things must be part of an object. Arrays reside on the heap, including arrays of primitive types, but in Java, arrays are objects too.
Besides the Java stack and the heap, the other place data may reside in the JVM is the method area, which contains all the class (or static) variables used by the program. The method area is similar to the stack in that it contains only primitive types and object references. Unlike the stack, however, the class variables in the method area are shared by all threads.
As described above, two memory areas in the Java virtual machine contain data shared by all threads. These are:
If multiple threads need to use the same objects or class variables concurrently, their access to the data must be properly managed. Otherwise, the program will have unpredictable behavior.
Read more of Bill's Under the Hood columns on JavaWorld
More about threads and concurrency
Inside the Java Virtual Machine (Bill Venners, McGraw-Hill Companies, January 2000) is reprinted in select chapters by Artima.com.
Also check out the JavaWorld site map and search engine.