The principle of java thread synchronization and the usage of wait, notify and the difference from sleep

The principle of java


thread synchronization and the usage of wait, notify and the difference from sleep
When called, the object's monitor will be responsible for handling concurrent exclusive requirements for these accesses.
(2) When a thread calls an object's synchronization method, the JVM checks the object's monitor. If the monitor is not occupied, the thread has the ownership of the monitor and can continue to execute the synchronization method of the object; if the monitor is occupied by other threads, the thread will be suspended until the monitor is released.
(3) Each java object has a lock. When there are multiple threads accessing shared resources at the same time, Synchronize is required to control security. Synchronize is divided into synchronize method and synchronize block. When using synchronize block, it must be obtained explicitly. Object locks (like synchronize(object)) are not required for methods.


Analysis description:
1. Each object has its own lock. When the synchronization method is called (synchronize (object) to obtain the lock of the object), if the thread that cannot get the lock of the object will hang until the lock is obtained.
2. When waiting is called, the lock of the object will be released first when the thread is suspended.
3. notify. When another thread accesses synchronization, notify can be called to wake up the thread suspended by calling the wait method.
4. The sleep() function is a static function of the Thread class, which does not involve the concept of synchronization between threads, just to allow a thread to get a period of sleep time by itself. Will not release object locks (if locked)


Reference (the principle of java thread synchronization and the usage of wait, notify and the difference from sleep): http://blog.sina.com.cn/s/blog_4a7afc9f0100tmzw.html
Reference (the application of wait method and notify method of java thread): http://blog.csdn.net/liranke/article/details/12222265
Reference (Java thread synchronization (synchronized wait notify)): http://blog.csdn.net/weizhaozhe/article/details/3922647

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326828662&siteId=291194637