java concurrent programming notes six

Monitor concept

Java object header

32-bit virtual machine as an example

  • Ordinary objects
    Here Insert Picture Description

  • An array of objects
    Here Insert Picture Description

Wherein the structure is a Mark Word
Here Insert Picture Description
Monitor the following structure

Here Insert Picture Description

  • Monitor the beginning of the Owner is null
  • When the Thread-2 perform synchronized (obj) will set the owner Owner Monitor for Thread-2, Monitor Only one Owner
  • Thread-2 locked in the process, if Thread-3, Thread-4, Thread-5 also performs synchronized (obj), will enter EntryList BLOCKED
  • Thread-2 content after executing the synchronization code block, and then wake up threads waiting to EntryList lock contention, when noncompetitive fair
  • FIG Thread-0 WaitSet in, Thread-1 is obtained through the lock before, but the condition is not met thread enters WAITING state

note:

  • only enter the monitor must be synchronized above-mentioned effects of the same object
  • Without synchronized objects are not associated with the monitor, do not follow the above rules

A story:

Story role

  • Pharaoh - JVM

  • Small South - Thread

  • My daughter - Thread

  • Rooms - Object

  • The room door - Security lock - Monitor

  • The room door - a small Southern bag - Lightweight lock

  • The room door - engraved with the name of the small South - biased locking

  • Batch name re-engraved - a class revocation biased locking threshold reaches 20

  • Not engraved name - the bulk biased locking revocation of such an object, the class can not set the bias

    To ensure that a small South room calculation does not interfere with the others (atomicity), initially, he bolt locks, when a context switch, the door lock. In this way, even if he left, others can not enter the door, his job is safe.

    However, in many cases no one told him to compete for the right to use the room. My daughter is to use the room, but the use of time are staggered, small south during the day, at night with my daughter. Every time locked too much trouble, there is no easier way to do it?

    South and small daughter who talk a bit, agreed not to lock the door, but who used the room, who put his bag hung on the door, but their bag styles are the same, so each front door was looking through the bag, look at textbooks , if it is your own, it can be the door, locked unlock this province. In case the bag is not their own, then wait outside the door, and lock the door with the next notice to the other way.

    Later, my daughter back home, and for a long time will not be used this room. Every small south still hanging bag, turning bag, although more than easy to lock the door, but still find trouble.

    Thus, the small South altogether on the door engraved with his name: [small South exclusive room, other people do not use], next time to use the room, as long as the name is still, then that no one bothered, or can be used safely room. If there are other people use this room during this period, then the user will wipe a small Southern engraved names, upgrading to hang bags of the way.

    The students are on holiday back home, a small south swells in 20 rooms engraved with his name, which would like to enter into what. Later his vacation back home, and then my daughter came back (she also with these rooms), the result is too small south erased one by one engraved names, upgrading to hang bags of the way. Pharaoh think it costs a bit high, proposes a method for batch re-engraved names, he let my daughter do not hang bags, you can direct his name engraved on the door

    Later, more and more frequent phenomenon engraved name, can not stand the Pharaoh: Well, these rooms are not engraved the name, can only hang bags.

Advanced synchronized principle

1. Lightweight lock

Lightweight lock usage scenarios: If an object although multiple threads to be locked, but the lock time is staggered (ie, no competition), you can use to optimize the lightweight lock.
Here Insert Picture Description

2. Lock expansion

  • When the Thread-1 plus lightweight lock fails, expansion of the flow into the lock
    • Object Object Monitor is the application lock, let Object pointing heavyweight lock address
    • Then he had entered the EntryList BLOCKED Monitor
      Here Insert Picture Description
    • Thread-0 when the sync block exit unlocked using cas Mark Word values ​​are restored to the object header, failed. Then enter heavyweight unlocking process, namely to find the objects according to Monitor Monitor address, set the Owner is null, in the wake EntryList BLOCKED thread

3. Spin optimization

Heavyweight lock contention, they can also be used to optimize spin, spin success if the current thread (that is, this time holding the lock thread has pulled out of sync blocks, release the lock), then the current thread to avoid blocking.

Just as qualified for circulation

4. biased locking

Lightweight lock in the absence of competition (on their own thread), each reentrant still need to execute CAS operations.
Here Insert Picture Description
When you create an object:

  • If you turn biased locking (enabled by default), then the object is created, markword value of 0x05 and last 3 to 101, then it's thread, epoch, age are 0
  • Biased locking is the default delay will not take effect immediately when the program starts, if you want to avoid delays, you can add VM parameters -XX:BiasedLockingStartupDelay=0to disable delay
  • If you do not tend to open the lock, then the object is created, markword value of 0x01 and last 3 to 001, then it's hashcode, age are zero, will be assigned for the first time to use hashcode
Undo - call the object hashCode

Calling the hashCode object, but tend to lock object is stored in MarkWord thread id, if you call hashCode lead to biased locking is revoked

  • Lightweight lock will lock the record in the record hashCode
  • HashCode heavyweight lock will be recorded in the Monitor
Undo - other threads to use the object

When there are other threads biased locking objects will tend to lock escalation as a lightweight lock

Undo - call wait / notify

Batch weight bias

If the object is accessed by multiple threads though, but there is no competition, then swerved thread T1 objects tend to have an opportunity to re-T2, tend to be heavy objects Thread ID is reset
when the revocation biased locking threshold after more than 20 times, jvm it so I felt that I was not wrong favor of it, so will tend to re-lock the thread when to lock these objects

Batch revoked

After the withdrawal biased locking threshold more than 40 times, jvm would think so, do yourself a favor wrong, did not favor this. Thus the entire class of all objects will tend to become unavailable, the new object is not biased

5. Lock elimination

Lock coarsening
repeatedly lock to the same objects, leading to the occurrence of multiple reentrant thread lock roughening may be used to optimize the way, unlike the previously spoken broken lock granularity.

So we will be useless JVM will help to eliminate synchronized to accelerate the program is running.

Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104563406