Thursday, April 12, 2007

How JVM performs Thread Synchronization?

JVM associates a lock with an object or a class to achieve mutilthreading. A lock is like a token or privilege that only one thread can "possess" at any one time. When a thread wants to lock a particular object or class, it asks the JVM.JVM responds to thread with a lock maybe very soon, maybe later, or never. When the thread no longer needs the lock, it returns it to the JVM. If another thread has requested the same lock, the JVM passes the lock to that thread.If a thread has a lock,no other thread can access the locked data until the thread that owns the lock releases it.

The JVM uses locks in conjunction with monitors. A monitor is basically a guardian in that it watches over a sequence of code, making sure only one thread at a time executes the code.Each monitor is associated with an object reference. It is the responsibility of monitor to watch an arriving thread must obtain a lock on the referenced object.

When the thread leaves the block,it releases the lock on the associated object.A single thread is allowed to lock the same object multiple times.JVM maintains a count of the number of times the object has been locked. An unlocked object has a count of zero. When a thread acquires the lock for the first time, the count is incremented to one. Each time the thread acquires a lock on the same object, a count is incremented. Each time the thread releases the lock, the count is decremented. When the count reaches zero, the lock is released and made available to other threads.

In Java language terminology, the coordination of multiple threads that must access shared data is called synchronization. The language provides two built-in ways to synchronize access to data: with synchronized statements or synchronized methods.

The JVM does not use any special opcodes to invoke or return from synchronized methods. When the JVM resolves the symbolic reference to a method, it determines whether the method is synchronized. If it is, the JVM acquires a lock before invoking the method. For an instance method, the JVM acquires the lock associated with the object upon which the method is being invoked. For a class method, it acquires the lock associated with the class to which the method belongs. After a synchronized method completes, whether it completes by returning or by throwing an exception, the lock is released.

Two opcodes, monitorenter and monitorexit are used by JVM for accomplishing this task.

When monitorenter is encountered by the Java virtual machine, it acquires the lock for the object referred to by objectref on the stack. If the thread already owns the lock for that object, a count is incremented. Each time monitorexit is executed for the thread on the object, the count is decremented. When the count reaches zero, the monitor is released.

15 comments :

Anonymous said...

Hey! Thanx for this beautiful place of the Inet!!

Deepun on November 22, 2009 at 8:26 AM said...

Pleasure is all mine for such a lovely comment. I am humbled.

Thomas on January 4, 2010 at 9:00 AM said...

I agree... The level of detail in the explanations is something that you dont find elsewhere on the web

Anonymous said...

Hi,

I just came across to www.interviewjava.com from bing and I find www.interviewjava.com very interesting.
I hope, can make lot's of new friend here :)

Anonymous said...

I totally support that! Continue that way!

Sopan on April 29, 2010 at 3:02 AM said...

nice description about thread.....

Vivek on May 22, 2010 at 5:10 AM said...

how can a thread have multiple lock on same object?

What is the purpose to have multiple lock ?

Deepun on May 22, 2010 at 10:47 AM said...

a thread acquiring a lock on the object and calling another synchronized block of code within a synchronized block of code.

So I think it is doable but purpose of creating such a situation may depend upon the need of logic.

Anonymous said...

Hi,

I've spent many hours on this great site working for my job interview. Thank you very much for the valuable information!

Anonymous said...

Hey all,

I got a question Does anyone have experience with [url=http://www.workwithforex.info/?s=xr]automated forex trading[/url]?

I wondered because i read this on ezinearticles

: [url=http://ezinearticles.com/?A-Forex-Autotrader-Review-a-Day-Keeps-the-Scammer-Away&id=1458951]Article on forex autotrading and reviews[/url]

It seems pretty solid

Any comments?

Thanks in advance!

Anonymous said...

Really very use full site.

If you put all question and answers in a single screen really very useful and easy to read.

Anonymous said...

Dear Friend,
I came to know about this side from one of my friend. I found this is very informative in a simple way. In a day I can feel more confidence in me for java.
Its really nice....
Almighty god bless you...


Thanks...
Muhammad Akram (write2akram@hotmail.com)

Anonymous said...

Hello there,

I have a question for the webmaster/admin here at interviewjava.motionsforum.com.

May I use part of the information from this blog post right above if I give a link back to your site?

Thanks,
Harry

Anonymous said...

Really good and detailed java answers on this site.
Thanks for detailed explanation of thread and synchronization. It is too good.
Keep it up.

Anonymous said...

This really a very useful site... I just googled it!! Ques and Answers in the same page will help us a lot..

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker