一个线程有几个锁 使用synchronized和Lock对象获取对象锁
使用synchronized和Lock对象获取对象锁
对象的锁所有对象都自动含有单一的锁
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 javapackage 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(); } }
lishixinzhi/Article/program/Java/hx/201311/26317相关文章
- 死磕 java同步系列之ReentrantLock源码解析(二)——条件锁
- 下载万能钥匙wifi自动解锁 万能钥匙:key一词该怎么样
- 有五把钥匙五把锁 一把锁和一把钥匙的事
- id锁已经可以解锁了 苹果id锁定了知道帐号密码手机怎么解锁
- 智能锁没电了怎么开门 全自动智能锁如果出现故障,开不了锁怎么办?
- id锁已经可以解锁了 苹果6plus可不可以设置应用锁,访问应用时要输密码
- 能不能根据挂锁配钥匙 没有钥匙,能配锁的钥匙吗?
- 各种各样锁子图片 锁
- 电脑键盘锁了怎么打开 求一个可以锁住电脑的软体 别人只要已下载电脑就会锁屏,必须要密码才能开启
- 华为一键锁屏怎么设置 小米1S青春版设定了密码锁屏,输入密码就不知怎么解锁了
爱学记

微信收款码
支付宝收款码