Object与内部锁的使用基础
1.概述
锁可以划分无锁、偏向锁、轻量级锁、重量级锁
1.1.锁的对比
1.内部锁Synchronized缺点:
非公平的
容易产生饥饿现象
2.基于Lock(ReentrantLock)锁的优点:
能够响应中断
支持释放锁进入等待状态到将来的某个时间。
自己决定是否公平,公平锁实现成本比较高,需要维护一个有序队列,性能相对低下。
1.2.锁的配合分析
Object的监视器 wait、notify、notifyAll与synchronized关键字配合,实现等待、通知。
Condition接口也提供了类似Ojbect监视器的方法,与Lock配合可以实现等待、通知。细微的差别如下:Charpter 5.6
2.Sync锁的使用分析
2.1.Obect的等待、通知实例
2.1.1.数据结构
package com.tech.ability.concurrentart.charpter04.ObjectWaitNotifyOK2;
/**
-
Created by kikop on 2019/4/24.
*/
public class MyCoreData {
private boolean waitFlag=true;public boolean isWaitFlag() {
return waitFlag;
}public void setWaitFlag(boolean waitFlag) {
this.waitFlag = waitFlag;
}
}
2.1.2.等待线程
package com.tech.ability.concurrentart.charpter04.ObjectWaitNotifyOK2;
import com.tech.ability.mycommonutils.DateUtil;
/**
-
Created by kikop on 2019/4/6.
*/
public class WaitTask implements Runnable {private MyCoreData myCoreData;
private Object lock = null;public WaitTask(Object lock, MyCoreData myCoreData) {
this.lock = lock;
this.myCoreData = myCoreData;
}@Override
public void run() {
synchronized (this.lock) { //首先要获得Object的监视器(由synchronized保护)
while (myCoreData.isWaitFlag()) {
try {
System.out.println(DateUtil.getCurrentThreadInfo("flag is true,wait ..."));
this.lock.wait(); //添加不满足,继续wait,同时释放lock的锁,进入同步队列(synchronizedQueue)
System.out.println(DateUtil.getCurrentThreadInfo("wait trigger,I am happy!"));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
//条件满足,完成工作
System.out.println(DateUtil.getCurrentThreadInfo("task finish,flag is false!"));
}
}
}
2.1.3.通知线程
package com.tech.ability.concurrentart.charpter04.ObjectWaitNotifyOK2;
import com.tech.ability.mycommonutils.DateUtil;
import com.tech.ability.mycommonutils.SleepUtils;
/**
- Created by kikop on 2019/4/6.
*/
public class NotifyTask implements Runnable {
private MyCoreData myCoreData;
private Object lock = null;
public NotifyTask(Object lock, MyCoreData myCoreData) {
this.lock = lock;
this.myCoreData = myCoreData;
}
@Override
public void run() {
synchronized (this.lock) {
System.out.println(DateUtil.getCurrentThreadInfo("hold lock..."));
this.lock.notifyAll();
myCoreData.setWaitFlag(false);
SleepUtils.second(5); //5秒后释放锁,还在包围圈
System.out.println(DateUtil.getCurrentThreadInfo("release lock..."));
}
synchronized (this.lock) {
System.out.println(DateUtil.getCurrentThreadInfo("hold lock again..."));
SleepUtils.second(5);
System.out.println(DateUtil.getCurrentThreadInfo("release lock!"));
}
}
}
2.1.4.测试调用
package com.tech.ability.concurrentart.charpter04.ObjectWaitNotifyOK2;
import com.tech.ability.mycommonutils.DateUtil;
import java.util.concurrent.TimeUnit;
/**
- Bug
- Created by kikop on 2019/4/6.
*/
public class WaitNotifyExample {
public static void main(String[] args) throws InterruptedException {
//注意点:
//基本的数据类型,并不能保证线程之间的数据可见性(即使加volatile),这里加上对象包装
MyCoreData myCoreData = new MyCoreData();
Object lock = new Object();
Thread waitThread = new Thread(new WaitTask(lock,myCoreData), "WaitThread");
waitThread.start();
TimeUnit.SECONDS.sleep(1);
Thread notifyThread = new Thread(new NotifyTask(lock,myCoreData), "NotifyThread");
notifyThread.start();
System.out.println(DateUtil.getCurrentThreadInfo("main exit!"));
}
}
2.1.5.结果查看
"C:\Program Files\Java\jdk1.8.0_60\bin\java"
[2019-04-24 07:05:40]WaitThread:flag is true,wait ...
[2019-04-24 07:05:41] main:main exit!
[2019-04-24 07:05:41]NotifyThread:hold lock...
[2019-04-24 07:05:46]NotifyThread:release lock...
[2019-04-24 07:05:46]NotifyThread:hold lock again...
[2019-04-24 07:05:51]NotifyThread:release lock!
[2019-04-24 07:05:51]WaitThread:wait trigger,I am happy!
[2019-04-24 07:05:51]WaitThread:task finish,flag is false!
Process finished with exit code 0
参考
1.Java并发编程的艺术Charpter4.3.2