同步和对象监视器_Synchronization && Monitor
同步和对象监视器_Synchronization && Monitor
秋风醉了 发表于3年前
同步和对象监视器_Synchronization && Monitor
  • 发表于 3年前
  • 阅读 77
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 同步和对象监视器_Synchronization && Monitor

同步和对象监视器_Synchronization && Monitor

内容摘自:https://docs.oracle.com/javase/specs/

The Java programming language provides multiple mechanisms(多种机制) for communicating between threads. The most basic of these methods is synchronizationwhich 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===============

共有 人打赏支持
粉丝 215
博文 610
码字总数 434752
×
秋风醉了
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: