Java synchronized methods

本文详细解析了synchronized方法的作用:一是确保同一对象的synchronized方法不会被多个线程并发执行;二是保证方法执行后对象状态的变化对其他线程可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

  • First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

在sun的tutorial上,对synchronized methods有上面的解释。意思是一个method加了这个关键字之后, 第一,多个线程不能同时执行同一个对象的synchronized方法,只能串行。第二,在一个synchronized方法执行完后,保证了对这个对象做的所有改动都对其他线程可见。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值