一、Synchronized和Lock
- 【追根溯源】Sychronized 是Java内置的关键字,而Lock是一个类
- 【自动挡与手动挡】Sychronized 自动释放锁,而Lock是手动释放(unlock)。(假如没有手动释放,就会造成死锁)
- 【锁的状态】Sychronized 不能获取锁的状态,Lock可以;
- 【在一棵树上吊死】Sychronized 当一个线程获取资源的锁,那么另外想要该资源的线程会一直等待,而Lock有tryLock,超时放弃等待;
- 【锁的量】Sychronized 适用于锁少量的代码同步问题,Lock时候锁大量的同步问题;
- 【公平与否】Sychronized 可重入锁,不能终端,非公平;Lock 可重入锁,默认非公平,但是也可以自己设定公平。
二、生产者和消费者问题(重要!!!)
2.1 普通版本
//线程之间的通信问题:生产者消费者问题——P(通知)V(等待唤醒)操作
public class A {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
class Data{//资源类
private int number = 0;
//+1
public synchronized void increment() throws InterruptedException {
//判断等待
if(number !=0){
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//唤醒
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
//判断等待
if(number !=1){
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//唤醒
this.notifyAll();
}
}
问题有:存在两个以上的线程的话,就不能保证安全了——虚假唤醒问题
解决:将if改为while
2.2 Lock版本
Lock替换synchronized方法和语句的使用,Condition取代了对象监视器方法的使用。
传统同步三剑客:synchronized关键字、wait()(等待唤醒)、notifyAll()(唤醒)
Lock版本:先new一个condition,await()(等待唤醒),signal()(唤醒)
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;
public class LockProducer {
public static void main(String[] args) {
Data1 data1 = new Data1();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data1.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data1.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data1.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
data1.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Data1{//资源类
private int number = 0;
//首先用Lock代替synchronized
Lock lock = new ReentrantLock();
//然后用condition代替监视器的功能
Condition condition = lock.newCondition();
//+1
public void increment() throws InterruptedException {
//执行业务代码之前,先加锁
try{
lock.lock();
//判断等待
while(number !=0){
//等待
//用condition的方法代替wait
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//唤醒
//同样用condition的方法来代替notifyAll()
condition.signalAll();
}catch(Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
//-1
public void decrement() throws InterruptedException {
try{
lock.lock();
//判断等待
while(number !=1){
//等待
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//唤醒
condition.signalAll();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
//Lock模板
//首先用Lock代替Synchronized
Lock lock = new ReentrantLock();
//把condition引出来
Condition condition = lock.newCondition();
//给资源加锁,并用try-catch-finally (ctrl+Shift+t唤出try catch)
try{
lock.lock();
//二剑客——判断等待
while(判断条件){
condition.await();
}
//在这里加上业务代码(操作)
//三剑客——唤醒
condition.signalAll();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
2.3 利用condition精准唤醒(有序执行线程)
这个代码写第二次了,之前是抄答案的,这次是自己写出来的~
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionProducer {
public static void main(String[] args) {
D d = new D();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
d.printA();
} catch (Exception e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
d.printB();
} catch (Exception e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for(int i = 0;i < 10;i++){
try {
d.printC();
} catch (Exception e) {
e.printStackTrace();
}
}
},"C").start();
}
}
class D{
private Lock lock = new ReentrantLock();
//一个线程分配一个监视器,通过监视器来确定唤醒谁
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
int num = 1;
public void printA(){
try {
lock.lock();
//业务代码:判断等待——>执行——>通知
while(num!=1){
condition1.await();
}
System.out.println(Thread.currentThread().getName()+"==>B");
num=2;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB(){
try {
lock.lock();
while(num!=2){
condition2.await();
}
System.out.println(Thread.currentThread().getName()+"==>C");
num=3;
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC(){
try {
lock.lock();
while(num!=3){
condition3.await();
}
System.out.println(Thread.currentThread().getName()+"==>A");
num=1;
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}