Translate

Search This Blog

Showing posts with label JVM. Show all posts
Showing posts with label JVM. Show all posts

Saturday, 6 August 2011

How does JVM handle synchronization?

Introduction
We have seen how the JVM organizes the program into runtime data areas in my previous blog. We know that each thread has its own stack and that all the threads in the program share the heap. The heap contains all the objects created within the program including the thread. The method area contains all the class /static variables of a classes used by the program. These variables are available to all the threads in the program.

Now we know that we have two data areas which contains data shared by all threads.
1. the heap and 2. the method area

Monitors
So if two threads try to access the objects or class variables in these areas concurrently, then the data need to be properly managed else we will end up with inconsistent data. This situation can be handled through synchronization and java manages this through the use of 'monitor'. Hence monitor acts as a guardian over a piece of code, so that no two threads execute that code simultaneously.
Java's monitor supports two kinds of synchronization: mutual exclusion and cooperation

  • Mutual exclusion is achieved in JVM through the use of object or class blocks , they enable multiple threads to work independently on shared data without interfering with  each other. 
  • While Cooperation in JVM is achieved through the use of object wait, notify and notifyAll methods.

Each monitor is associated with an object reference. Whenever a thread reaches the code which is synchronized, it must obtain a lock on the referenced object failing which it will have to wait (block on synchronization state). Once the thread obtains the lock the JVM increases the count of the number of times an object has been locked. The same thread can lock the same object multiple times. Whenever the thread releases/relinquishes the lock the count is decremented. When there are no more locks on the object i.e count returns to zero, then any other thread can obtain the lock on the object if needed.

Synchronization is supported by the java language  in two ways

  1. synchronized method : Whenever the JVM encounters a symbolic reference to the method and realizes its a synchronized method, then it tries to obtains the lock from the monitor. If the lock is obtained then the synchronized method is executed and once all the statements are processed or the code throws an exception the lock is released. For an instance method, a lock is obtained on the object, of which synchronized method is invoked. In case of static synchronized method the lock is obtained on the class object. The JVM does not use special op-codes to invoke or return from method level synchronization.
  2. synchronized statement (Block of code): The JVM uses two special op codes monitorenter and monitorexit whenever a thread enters or exits the synchronized block of code. When the JVM's encounters monitorenter  it tries to obtain a lock on the object referred to by objectref on the stack. If the lock is already obtained the the count is incremented by one and whenever the monitorexit  is encountered the count is decremented by one. When the count reaches zero the lock is released.

References



Wednesday, 22 June 2011

JVM

Java Virtual machine -is an abstract computing machine. It is the component of java technology that gives it hardware and OS independence. It has its own instruction set and manipulates various memory areas at run time.

The architecture as defined in the specification makes it possible to have different JVM implementation for different hardware platforms.The architecture of the JVM as defined in the specification is shown in the diagram below

It consists of
  1. Class Loader subsystem
  2. Run time data areas
  3. Execution engine
  4. Native method interface
  5. Native method libraries.
Class loader subsystem - is responsible for loading types (classes and interfaces). The .class files adhere to fix format and it contains JVM instructions or byte codes, a symbol table and other ancillary information.
Execution engine - is responsible for the actual execution of the instructions contained in the method of the loaded classes.
Run time data areas - the JVM organizes the memory it needs to execute a programs into various data areas. These various data areas may be shared by all the threads in the program or some data areas may be thread specific
  • The method area and heap is shared by all the threads
  • where as the PC registers,java stack, native method stack are thread specific.
Each instance of JVM has one memory area and one heap.
  • Method area: holds the details of each class loaded by the class loader subsystem.
  • Heap: holds every object being created by the threads during execution, including the main thread.

As each thread executes in the JVM, it gets its own PC register and the java stack. They are not shared among threads.

  • PC registers: For any executing thread the register holds the address of the next instruction to execute.
  • Java Stack: the stack stores the state of the method invocation (state - includes the local variables, the method parameters, the return value if any and intermediate or temporary calculations) of the thread (in stack frames - current execution method stack frame is the topmost)
  • Native method stack : the state of the native method are stored in native method stacks in an implementation dependent way, so also possibly the registers or an other implementation dependent memory areas.

More on class loader subsystem in my next blog.

You can find useful references


Java Platform

is a platform for java development. It is a software only platform which is ported on hardware platforms It comprises of two major components
  1. JVM - Java virtual machine - it is the component which add portability aspect to Java. JVM understand byte codes (compiled .class files from .java classes). Byte codes are the machine language of the JVM. JVM then interprets byte codes to the actual underlying machine language. Compilation happens only once and interpretation happens each time the program is executed.
  2. Java API - a set of classes written using java and which runs on the JVM. It consists of lot of useful classes which help us in development of applications etc.
You can find useful references