知识大全 Java:使用synchronized和Lock对象获取对象锁
Posted hr
篇首语:节俭是致富的秘诀。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java:使用synchronized和Lock对象获取对象锁相关的知识,希望对你有一定的参考价值。
Java:使用synchronized和Lock对象获取对象锁 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
在并发环境下 解决共享资源冲突问题时 可以考虑使用锁机制
对象的锁
所有对象都自动含有单一的锁
JVM负责跟踪对象被加锁的次数 如果一个对象被解锁 其计数变为 在任务(线程)第一次给对象加锁的时候 计数变为 每当这个相同的任务(线程)在此对象上获得锁时 计数会递增
只有首先获得锁的任务(线程)才能继续获取该对象上的多个锁
每当任务离开一个synchronized方法 计数递减 当计数为 的时候 锁被完全释放 此时别的任务就可以使用此资源
synchronized同步块
同步到单一对象锁
当使用同步块时 如果方法下的同步块都同步到一个对象上的锁 则所有的任务(线程)只能互斥的进入这些同步块
Resource java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中 虽然这些同步块处在不同的方法中 但由于是同步到同一个对象(当前对象 synchronized (this)) 所以对它们的方法依然是互斥的
Resource java
package zj lock;
import ncurrent TimeUnit;
public class Resource
public void f()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in f() );
synchronized (this)
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in f() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public void g()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in g() );
synchronized (this)
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in g() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public void h()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in h() );
synchronized (this)
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in h() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public static void main(String[] args)
final Resource rs = new Resource ();
new Thread()
public void run()
rs f();
start();
new Thread()
public void run()
rs g();
start();
rs h();
结果
Thread :not synchronized in f()
Thread :synchronized in f()
main:not synchronized in h()
Thread :not synchronized in g()
Thread :synchronized in f()
Thread :synchronized in f()
Thread :synchronized in f()
Thread :synchronized in f()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
同步到多个对象锁
Resource java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中 这些同步块处在不同的方法中 并且是同步到三个不同的对象(synchronized (this) synchronized (syncObject ) synchronized (syncObject )) 所以对它们的方法中的临界资源访问是独立的
Resource java
package zj lock;
import ncurrent TimeUnit;
public class Resource
private Object syncObject = new Object();
private Object syncObject = new Object();
public void f()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in f() );
synchronized (this)
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in f() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public void g()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in g() );
synchronized (syncObject )
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in g() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public void h()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in h() );
synchronized (syncObject )
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in h() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
public static void main(String[] args)
final Resource rs = new Resource ();
new Thread()
public void run()
rs f();
start();
new Thread()
public void run()
rs g();
start();
rs h();
结果
Thread :not synchronized in f()
Thread :synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread :not synchronized in g()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Lock对象锁
除了使用synchronized外 还可以使用Lock对象来创建临界区 Resource java的演示效果同Resource java Resource java的演示效果同Resource java
Resource java
package zj lock;
import ncurrent TimeUnit;
import ncurrent locks Lock;
import ncurrent locks ReentrantLock;
public class Resource
private Lock lock = new ReentrantLock();
public void f()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in f() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in f() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public void g()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in g() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in g() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public void h()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in h() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in h() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public static void main(String[] args)
final Resource rs = new Resource ();
new Thread()
public void run()
rs f();
start();
new Thread()
public void run()
rs g();
start();
rs h();
结果
Thread :not synchronized in f()
Thread :synchronized in f()
main:not synchronized in h()
Thread :not synchronized in g()
Thread :synchronized in f()
Thread :synchronized in f()
Thread :synchronized in f()
Thread :synchronized in f()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
Thread :synchronized in g()
Resource java
package zj lock;
import ncurrent TimeUnit;
import ncurrent locks Lock;
import ncurrent locks ReentrantLock;
public class Resource
private Lock lock = new ReentrantLock();
private Lock lock = new ReentrantLock();
private Lock lock = new ReentrantLock();
public void f()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in f() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in f() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public void g()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in g() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in g() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public void h()
// other operations should not be locked
System out println(Thread currentThread() getName()
+ :not synchronized in h() );
lock lock();
try
for (int i = ; i < ; i++)
System out println(Thread currentThread() getName()
+ :synchronized in h() );
try
TimeUnit SECONDS sleep( );
catch (InterruptedException e)
e printStackTrace();
finally
lock unlock();
public static void main(String[] args)
final Resource rs = new Resource ();
new Thread()
public void run()
rs f();
start();
new Thread()
public void run()
rs g();
start();
rs h();
结果
Thread :not synchronized in f()
Thread :synchronized in f()
main:not synchronized in h()
main:synchronized in h()
Thread :not synchronized in g()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
Thread :synchronized in g()
Thread :synchronized in f()
main:synchronized in h()
cha138/Article/program/Java/hx/201311/26181相关参考
Oracle]浅谈Lock与Latch 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!以下是对Ora
C#多线程中lock的用法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 经常碰到同时需要对某个
Java多线程同步-BusyFlag或Lock 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 我
SQL7中LOCK的理解(2) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 下面介绍的是锁的并
Oracle数据库监看lockscript 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Scr
案例类别VAS网络系统类型CMODE系统版本硬件SUN软件所有版本案例标题CMODE放号中的数据库出现LOCK的处理方法故障现象启动sam_cmode进程不能正常处理工单故障描述启动以sam_cm
上一次在使用Java开始面向对象的编程这篇文章中我们学习了一个编程语言要真正成为面向对象的它应该支持信息隐藏/封装多态继承和动态绑定另外我们知道了Java完全支持这些功能而且知道了因为Java是一
数据集组件Table用于与数据表连接如与学生档案表XA连接数据表通常由若干个字段组成如XA由XAXA…XA共个字段组成实现对数据表中各个字段的数据处理就要用到字段对象TField TField对
抽象类 继承层越高类就更通用并且更抽象有些层中的祖先类非常通用更适于作为其他类的框架而不适于作为具体类来使用其特定实例
新四板的服务对象和功能新四板服务对象新四板的服务对象尚未进入成熟期但具有成长潜力且满足有关规范性要求的科技创新中小企业,认定原则如下:遵循市场化认定原则,主要通过企业充分披露“科技创新”特征,推荐机构