1 同步锁
- 用于解决多线程安全问题的方式
- 同步代码块
- 同步方法
- 同步锁(Java1.5之后),是一个显式的锁,需要通过
lock()
方法上锁,必须通过unlock()
方法释放锁,为了保证一定执行,一般放在finally
代码块中
package JUC;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 模拟抢票
*
* @author Yorick
*
*/
public class LockTest {
public static void main(String[] args) {
TicketSaler saler = new TicketSaler();
new Thread(saler, "Windows_1:").start();
new Thread(saler, "Windows_2:").start();
new Thread(saler, "Windows_3:").start();
}
}
class TicketSaler implements Runnable {
private int ticketNum = 100;
// 创建锁的实例
private Lock lock = new ReentrantLock();
@Override
public void run() {
// 当一个线程启动该方法时上锁
this.lock.lock();
try {
while (this.ticketNum > 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + --this.ticketNum);
}
} finally {
// 当一个线程结束抢票过程后,解锁
this.lock.unlock();
}
}
}
2 生产者消费者案例——虚假唤醒
2.1 引入
- 一个标准的生产者消费者实现:此时生产者消费者均生产消费20次(生产数量等于消费数量)
package JUC;
/**
* 生产者消费者模拟
*
* @author Yorick
*
*/
public class ProducerAndConsumer {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer producer = new Producer(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(producer,"生产者").start();
new Thread(consumer,"消费者").start();
}
}
/**
* 店员:具备进货和出货的能力
*
* @author Yorick
*
*/
class Clerk {
private int goodsNum;
public synchronized void getGoods() {
if (this.goodsNum >= 10) {
System.out.println("商品已满!");
try {
// 商品数量已满时,生产者线程等待,不再进行生产
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("从" + Thread.currentThread().getName() + "进货,库存还有" + ++this.goodsNum + "件");
// 商品数量不足10件时,唤醒生产者线程进行生产
this.notifyAll();
}
}
public synchronized void saleGoods() {
if (this.goodsNum <= 0) {
System.out.println("缺货!");
try {
// 商品数量不足时,消费者线程等待,不再进行消费
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("卖给" + Thread.currentThread().getName() + ",库存还有" + --this.goodsNum + "件");
// 商品数量充足时,唤醒消费者线程进行消费
this.notifyAll();
}
}
}
/**
* 生产者:生产产品
*
* @author Yorick
*
*/
class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.clerk.getGoods();
}
}
}
/**
* 消费者:消耗产品
*
* @author Yorick
*
*/
class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.clerk.saleGoods();
}
}
}
package JUC;
/**
* 生产者消费者模拟
*
* @author Yorick
*
*/
public class ProducerAndConsumer {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer producer = new Producer(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(producer, "生产者").start();
new Thread(consumer, "消费者").start();
}
}
/**
* 店员:具备进货和出货的能力
*
* @author Yorick
*
*/
class Clerk {
private int goodsNum;
public synchronized void getGoods() {
if (this.goodsNum >= 1) {
System.out.println("商品已满!");
try {
// 商品数量已满时,生产者线程等待,不再进行生产
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("从" + Thread.currentThread().getName() + "进货,库存还有" + ++this.goodsNum + "件");
// 商品数量不足10件时,唤醒生产者线程进行生产
this.notifyAll();
}
}
public synchronized void saleGoods() {
if (this.goodsNum <= 0) {
System.out.println("缺货!");
try {
// 商品数量不足时,消费者线程等待,不再进行消费
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("卖给" + Thread.currentThread().getName() + ",库存还有" + --this.goodsNum + "件");
// 商品数量充足时,唤醒消费者线程进行消费
this.notifyAll();
}
}
}
/**
* 生产者:生产产品
*
* @author Yorick
*
*/
class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.clerk.getGoods();
}
}
}
/**
* 消费者:消耗产品
*
* @author Yorick
*
*/
class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.clerk.saleGoods();
}
}
}
- 此时,店员只有一个位置供生产者生产消费者消费,且生产速度比消费速度低(获取执行权后消费的次数会多于生产的次数)
- 现象:程序无法结束
- 原因:假设消费者线程中的循环进入最后一次,此时由于生产者较慢,还有两轮循环没有走完,此时消费者要消费,但是没有商品,输出“缺货”后挂起等待(此时在if语句块中)。
- 解决办法:去掉消费者和生产者的else分支保证每次执行都能唤醒另外一个线程
2.2 虚假唤醒
package JUC;
/**
* 生产者消费者模拟
*
* @author Yorick
*
*/
public class ProducerAndConsumer {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer producer = new Producer(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(producer, "生产者_1").start();
new Thread(consumer, "消费者_1").start();
new Thread(producer, "生产者_2").start();
new Thread(consumer, "消费者_2").start();
}
}
/**
* 店员:具备进货和出货的能力
*
* @author Yorick
*
*/
class Clerk {
private int goodsNum;
public synchronized void getGoods() {
while (this.goodsNum >= 1) {
System.out.println("商品已满!");
try {
// 商品数量已满时,生产者线程等待,不再进行生产
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("从" + Thread.currentThread().getName() + "进货,库存还有" + ++this.goodsNum + "件");
// 商品数量不足10件时,唤醒生产者线程进行生产
this.notifyAll();
}
public synchronized void saleGoods() {
while (this.goodsNum <= 0) {
System.out.println("缺货!");
try {
// 商品数量不足时,消费者线程等待,不再进行消费
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("卖给" + Thread.currentThread().getName() + ",库存还有" + --this.goodsNum + "件");
// 商品数量充足时,唤醒消费者线程进行消费
this.notifyAll();
}
}
/**
* 生产者:生产产品
*
* @author Yorick
*
*/
class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.clerk.getGoods();
}
}
}
/**
* 消费者:消耗产品
*
* @author Yorick
*
*/
class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.clerk.saleGoods();
}
}
}
- 保持上边的条件不变,创建多个生产者和消费者的线程
- 现象:这行结果中出现负值
- 原因:当有多个生产者消费者线程同时执行的时候,如果两个消费者都访问到了
saleGoods()
方法,且此时商品数量为0,则两个消费者线程均处于等待的状态,但是生产者一次性只生产一个,所以当两个消费者线程再次启动的时候,一个抢到了产品,另一个抢到了-1号产品,此时就会产生错误。 - 解决办法:将if结构改成while结构,保证每次
wait()
执行后再次进行商品数量的判断 - JDK API原文:
3 Condition线程通讯——生产者消费者模式升级
Condition
接口描述了可能会与锁有关联的条件变量。这些变量在用法上与使用Object.wait
访问的隐式监视器类似,但提供了更强大的功能。需要特别指出的是,单个Lock可能与多个Condition对象关联。为了避免兼容性问题,Condition方法的名称与对应的Object版本中的不同。- 在Condition对象中,与
wait()
、notify
()和notifyAll()
方法对应的分别是await()
、signal()
和signalAll()
。 - Condition实例实质上被绑定到一个锁上。要为特定Lock实例获得Condition实例,请使用其
newCondition()
方法。
package JUC;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 生产者消费者模拟
*
* @author Yorick
*
*/
public class ProducerAndConsumer {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer producer = new Producer(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(producer, "生产者_1").start();
new Thread(consumer, "消费者_1").start();
new Thread(producer, "生产者_2").start();
new Thread(consumer, "消费者_2").start();
}
}
/**
* 店员:具备进货和出货的能力
*
* @author Yorick
*
*/
class Clerk {
private int goodsNum;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void getGoods() {
lock.lock();
try {
while (this.goodsNum >= 1) {
System.out.println("商品已满!");
try {
// 商品数量已满时,生产者线程等待,不再进行生产
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("从" + Thread.currentThread().getName() + "进货,库存还有" + ++this.goodsNum + "件");
// 商品数量不足10件时,唤醒生产者线程进行生产
condition.signalAll();
} finally {
lock.unlock();
}
}
public void saleGoods() {
lock.lock();
try {
while (this.goodsNum <= 0) {
System.out.println("缺货!");
try {
// 商品数量不足时,消费者线程等待,不再进行消费
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("卖给" + Thread.currentThread().getName() + ",库存还有" + --this.goodsNum + "件");
// 商品数量充足时,唤醒消费者线程进行消费
condition.signalAll();
} finally {
lock.unlock();
}
}
}
/**
* 生产者:生产产品
*
* @author Yorick
*
*/
class Producer implements Runnable {
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.clerk.getGoods();
}
}
}
/**
* 消费者:消耗产品
*
* @author Yorick
*
*/
class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.clerk.saleGoods();
}
}
}
4 线程按序交替
package JUC;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 编写一个程序,开启3 个线程,这三个线程的ID 分别为A、B、C,每个线程将自己的ID 在屏幕上打印10 遍,要求输出的结果必须按顺序显示。
* 如:ABCABCABC…… 依次递归
*
* @author Yorick
*
*/
public class AlternateABC {
public static void main(String[] args) {
Alternate alternate = new Alternate();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
alternate.loopA();
}
}
}, "A").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
alternate.loopB();
}
}
}, "B").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
alternate.loopC();
}
}
}, "C").start();
}
}
class Alternate {
private int number = 1;
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
public void loopA() {
lock.lock();
try {
while (this.number != 1) {
condition1.await();
}
System.out.print(Thread.currentThread().getName());
this.number = 2;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopB() {
lock.lock();
try {
while (this.number != 2) {
condition2.await();
}
System.out.print(Thread.currentThread().getName());
this.number = 3;
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopC() {
lock.lock();
try {
while (this.number != 3) {
condition3.await();
}
System.out.print(Thread.currentThread().getName());
this.number = 1;
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}