Synchronization_Monitor_동기화 및 객체 모니터
컨텐트 출처:https://docs.oracle.com/javase/specs/
Java programming language provides multiple mechanisms for communication between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.
The synchronized statement (§14.19) computes a reference to an object; it then attempts to perform a lock action on that object's monitor and does not proceed further until the lock action has successfully completed. After the lock action has been performed, the body of the synchronized statement is executed. If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.
A synchronized method (§8.4.3.6) automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined. If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.
아래 코드와 같이
package com.usoft;
/**
* If the method is an instance method,
* it locks the monitor associated with the instance for which it was invoked
* (that is, the object that will be known as this during execution of the body of the method).
* If the method is static, it locks the monitor associated with the Class object
* that represents the class in which the method is defined.
* Created by liyanxin on 2015/1/5.
*/
public class HelloWorld {
// HelloWorld.class
synchronized public static void methodA1() {
}
// methodA1
public static void methodA2() {
synchronized (HelloWorld.class) {
}
}
// , this
synchronized public void methodB1() {
}
// methodB1
public void methodB2() {
synchronized (this) {
}
}
}
The Java programming language neither prevents nor requires detection(검사) of deadlock conditions.Programs where threads hold (directly or indirectly) locks on multiple objects should use conventional techniques for deadlock avoidance,creating higher-level locking primitives that do not deadlock, if necessary.Other mechanisms, such as reads and writes of volatile variables and the use of classes in the java.util.concurrent package,provide alternative(대체, 예비)ways of synchronization.
===============END===============
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.