Header Ads

  • Breaking Now

    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.


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

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

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

    4. 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 :)

    5. I totally support that! Continue that way!

    6. nice description about thread.....

    7. how can a thread have multiple lock on same object?

      What is the purpose to have multiple lock ?

    8. 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.

    9. Hi,

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

    10. 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!

    11. Really very use full site.

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

    12. 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...

      Muhammad Akram (write2akram@hotmail.com)

    13. 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?


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

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


    Post Top Ad

    Post Bottom Ad