Skip Headers
Oracle® JRockit Introduction
Release R28

Part Number E15058-05
Go to Documentation Home
Home
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

3 Understanding Threads and Locks

This chapter contains basic information about threads and locks in the JRockit JVM.

A running application usually consists of one process with its own memory space. A computer generally runs several processes at the same time. For example, a word processor application process might run at the same time as a media player application process. A process consists of many threads that run concurrently. When you run a Java application, a new JVM process starts.

Each Java process has at least one application thread. In addition to the application threads, the Oracle JRockit JVM uses internal threads for garbage collection, code generation, and other internal purposes.

You can use thread dumps (printouts of the stacks of all the threads in an application) to diagnose problems and optimize application and JVM performance. For information about generating thread dumps, see "Using Thread Dumps" in Oracle JRockit JDK Tools.

The following sections describe the threads and locks in the JRockit JVM:

3.1 Understanding Threads

A Java application consists of one or more threads that run Java code. The JVM process consists of the Java threads and some JVM-internal threads, for example one or more garbage collection threads, a code optimizer thread, and one or more finalizer threads.

The operating system handles Java threads as it does any application thread. Thread scheduling and priorities are handled by the operating system.

Within Java, the Java threads are represented by thread objects. Each thread also has a stack, used for storing run-time data. The thread stack has a specific size. If a thread tries to store more items on the stack than the stack size allows, the thread throws a stack overflow error.

Default Stack Sizes for Java Application Threads

The default stack sizes vary depending on the operating system you use. Table 3-1 lists the default stack sizes for different operating systems.

Table 3-1 Default Stack Size

Operating System Default Stack Size

Windows IA32

64 KB

Windows x64

128 KB

Linux IA32

128 KB

Linux x64

256 KB

Solaris/SPARC

512 KB


You can change the thread stack size with the -Xss command-line option (for example, java -Xss:512k MyApplication

Default Stack Size for JVM Internal Threads

A special "system" stack size (256 KB on all platforms) is used for JVM internal threads such as the garbage collection and code generation threads.

Note:

The -Xss command-line option sets the stack size of both application threads and JVM internal threads.

3.2 Understanding Locks

When threads in a process share and update the same data, their activities must be synchronized to avoid errors. In Java, this is done with the synchronized keyword, or with the wait and notify keywords. Synchronization is achieved by the use of locks, each of which is associated with an object. For a thread to work on an object, it must control (hold) the associated lock. Only one thread can hold a lock at a time. If a thread tries to take control of a lock that is already held by another thread, then it must wait until the lock is released. When this happens, there is contention for the lock.

Locks can be of four types:

The JRockit JVM uses a complex set of heuristics to determine when to change from one type of lock to another.

Lock Chains

Several threads can be tied up in what is called a lock chain. A lock chain can be defined as follows:

Lock Chain Types

The JRockit JVM analyzes the threads and forms complete lock chains. There are three possible types of lock chains: open, deadlocked and blocked.