Concurrent programming -synchronized

What synchronized lock?

 Lock object, which may include: this, the critical resource object, class class object

Lock the underlying implementation

Sync block synchronization based monitor enter and monitor exit commands,

Implicitly implement synchronization method based on the method of reading the flag ACC_SYNCHRONIZED runtime constant pool.

The composition of the object memory

Object header: hashcode \ lock information \ the GC information \ storage object pointer type

Instance variables

Padding data: the start address of the target virtual machine requirements must be an integer multiple of 8 bytes. Padding data need not be present, only for byte alignment.

Implementation process

When performing synchronized or synchronized block synchronization method, the lock mark records, monitor lock object mark pointing to the starting address in the object header.

monotor objects: Each object associated with the presence of a monitor, monitor can create or destroy automatically generated when a thread attempts to acquire the object lock together with the object. When the monitor object is held by a thread, it is in a locked state

ObjectMonotor monitor is implemented. ObjectMonitor comprises: _owner marker, two queues, _waitSet \ _entryList

When the thread concurrently access the same synchronization code, first enters _entryList, when the thread acquires the lock flag, _owner record this thread, the monitor and the +1 counter operation is performed on behalf of the lock. And continues to block other threads in _entryList in.

If the execution thread calls the wait method, the counter is set in the monitor is 0, _owner set to give up lock is null, on behalf of, a thread of execution entered in waitset blocked.

If the execution thread calls notify \ notifyAll, _waitset the thread wakes up, enter _entryList in blocked waiting to acquire a lock mark.

If the synchronization code execution thread execution ends, will release the lock mark, the counter is set to monitor the 0, _owner set to null

 

Guess you like

Origin www.cnblogs.com/yintingting/p/11424306.html
Recommended