一.什么是JDK 1.5 Lock锁
我们都用过同步函数,知道同步代码块的锁是任意对象,同步方法的锁是this对象,静态方法的锁是类的字节码文件对象。
但是但是我们并没有直接看到在哪里加了锁,在哪里释放了锁,为了更清晰的表达如何加锁和释放锁,JDK5中提供了一个新的锁对象Lock。
有了lock锁后可以明确知道在哪里上锁及释放锁。
二.作用
手动锁,保证线程安全问题,灵活性高
三.Lock与Synchronized的区别
Synchronized自动化的一把锁,缺点:效率低,扩展性不能自定义。
Lock手动开始上锁,灵活性高
实例:
package com.company.emple2;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Res {
public String userName;
public String sex;
//true 生产者等待 消费者可以消费 false生产者消费,消费者等待
public boolean flag = false;
//lock锁
Lock lock = new ReentrantLock();
}
class InputThread extends Thread {
private Res res;
public InputThread(Res res) {
this.res = res;
}
@Override
public void run() {
int count = 0;
while (true) {
res.lock.lock();//表示开始上锁
try {
if (count == 0) {
res.userName = "余胜军";
res.sex = "男";
} else {
res.userName = "小红";
res.sex = "女";
count = (count + 1) % 2;
res.flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
res.lock.unlock();//表示释放锁
}
}
}
}
class OutThread extends Thread {
private Res res;
public OutThread(Res res) {
this.res = res;
}
@Override
public void run() {
while (true) {
try {
res.lock.lock();
System.out.println(res.userName + "," + res.sex);
res.flag = false;
} catch (Exception e) {
e.printStackTrace();
} finally {
res.lock.unlock();
}
}
}
}
public class OutInputThread {
public static void main(String[] args) {
Res res = new Res();
InputThread inputThread = new InputThread(res);
OutThread outThread = new OutThread(res);
inputThread.start();
outThread.start();
}
}
运行:
可以看到解决了线程安全的问题
因为Wait, noitifi只能在Synchronized里面使用,不能在并发包中使用。
那么如何解决生产者,消费者问题呢?
办法:使用Condition类,该类的作用类似于在传统的线程中的wait方法和notify方法。
在上锁前加入Condition condition = res.lock.newCondition();
condition.await();类似于Object的wait方法
condition.signal();类似与Object的notifi方法
实例:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Res {
public String userName;
public String sex;
public boolean flag = false;
Lock lock = new ReentrantLock();
}
class InputThread extends Thread {
private Res res;
Condition newCondition;
public InputThread(Res res, Condition newCondition) {
this.res = res;
this.newCondition=newCondition;
}
@Override
public void run() {
int count = 0;
while (true) {
// synchronized (res) {
try {
res.lock.lock();
if (res.flag) {
try {
// res.wait();
newCondition.await();
} catch (Exception e) {
// TODO: handle exception
}
}
if (count == 0) {
res.userName = "余胜军";
res.sex = "男";
} else {
res.userName = "小红";
res.sex = "女";
}
count = (count + 1) % 2;
res.flag = true;
// res.notify();
newCondition.signal();
} catch (Exception e) {
// TODO: handle exception
}finally {
res.lock.unlock();
}
}
// }
}
}
class OutThrad extends Thread {
private Res res;
private Condition newCondition;
public OutThrad(Res res,Condition newCondition) {
this.res = res;
this.newCondition=newCondition;
}
@Override
public void run() {
while (true) {
// synchronized (res) {
try {
res.lock.lock();
if (!res.flag) {
try {
// res.wait();
newCondition.await();
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println(res.userName + "," + res.sex);
res.flag = false;
// res.notify();
newCondition.signal();
} catch (Exception e) {
// TODO: handle exception
}finally {
res.lock.unlock();
}
// }
}
}
}
public class OutInputThread {
public static void main(String[] args) {
Res res = new Res();
Condition newCondition = res.lock.newCondition();
InputThread inputThread = new InputThread(res,newCondition);
OutThrad outThrad = new OutThrad(res,newCondition);
inputThread.start();
outThrad.start();
}
}
运行截图:
可以看到解决了这个问题