生产者和消费者及8锁现象
Posted 偶像java练习生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生产者和消费者及8锁现象相关的知识,希望对你有一定的参考价值。
JUC生产者和消费者
- 通过Lock 找到Condition
代码实现下:
package com;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class B {
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();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
// 判断等待,业务,通知
class Data2 { //数字 资源类
private int number = 0;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
// condition.await();//等待
// condition.signalAll(); //唤醒全部
//+1
public synchronized void increment() throws InterruptedException {
lock.lock();
try {
while (number != 0) {
//等待操作
condition.await();
// System.out.println(Thread.currentThread().getName() + "wait=>" + number);
// this.wait();
}
number++;
System.out.println(Thread.currentThread().getName() + "=>" + number);
//通知其他线程,我+1 完毕了
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
//-1
public synchronized void decrement() throws InterruptedException {
lock.lock();
try {
while (number == 0) {
//等待
// System.out.println(Thread.currentThread().getName() + "wait=>" + number);
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName() + "=>" + number);
// 通知其他线程,我-1 完毕了
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
任何一个新的技术,绝对不是仅仅只是复覆盖原来的技术,应该增加优势和补充!
Condition 精准的通知和唤醒线程
代码测试:
package com;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* A 执行完调用B,B 执行完调用C ,C执行完调用A
*/
public class C {
public static void main(String[] args) {
Data4 data4 = new Data4();
new Thread(() -> { for (int i = 0; i < 10; i++) data4.printA();}, "A").start();
new Thread(() -> { for (int i = 0; i < 10; i++) data4.printB();}, "B").start();
new Thread(() -> { for (int i = 0; i < 10; i++) data4.printC();}, "C").start();
}
}
class Data4{ //资源类 Lock
private Lock lock = new ReentrantLock();
//同步监视器,通过监视器唤醒哪一个
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
private int number =1; //1A 2B 3C
public void printA(){
lock.lock();
try {
//业务,判断是否等待过程-> 执行->通知
while(number!=1){
//等待
condition1.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAAAAAAAAA");
//唤醒,唤醒指定的人B
number =2;
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.lock();
}
}
public void printB(){
lock.lock();
try {
//业务,判断是否等待过程-> 执行->通知
while (number != 2){
condition2.await();
}
System.out.println(Thread.currentThread().getName()+"=>BBBBBBBBBBB");
number=3;
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC(){
lock.lock();
try {
//业务,判断是否等待过程-> 执行->通知
while(number!=3){
condition3.await();
}
System.out.println(Thread.currentThread().getName()+"=>CCCCCCCCCCCCC");
number =1;
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
//生产线: 下单->支付->交易->物流
}
输出结果:
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
B=>BBBBBBBBBBB
C=>CCCCCCCCCCCCC
A=>AAAAAAAAAAA
5. 8 锁现象
.如何判断锁的是谁!永远的知道什么锁,锁到底锁的是谁!
锁的是对象和 Class 模板
深刻理解我们的锁
package com.lock8;
import java.util.concurrent.TimeUnit;
/**
* 8锁,就是关于锁的8个问题
* 1.标准情况下,两个线程先打印,发短信还是 打电话? 1/发短信 2/打电话
*
*
*/
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
//phone.sendSms; 不能回答sendSms 是先调用,锁的问题
new Thread(()->{ phone.sendSms(); },"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{ phone.call(); },"B").start();
}
}
class Phone{
//synchronized 锁的对象是方法的调用者!
//两个方法用的是同一个锁,谁先拿到谁执行!
public synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
输出结果:
发短信
打电话
package com.lock8;
import java.util.concurrent.TimeUnit;
/**
* 3.增加了一个普通方法后!先执行发短信还是Hello,普通方法
* 4.两个对象,两个同步方法,发短信还是打电话? 打电话,打短信
*/
public class Test2 {
public static void main(String[] args) {
//两个对象,两个调用者,两把锁
Phone2 phone1 = new Phone2();
Phone2 phone2 = new Phone2();
//phone.sendSms; 不能回答sendSms 是先调用,锁的问题
new Thread(()->{ phone1.sendSms(); },"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// new Thread(()->{ phone.call(); },"B").start();
new Thread(()->{phone2.call();},"B").start();
}
}
class Phone2{
//synchronized 锁的对象是方法的调用者!
//两个方法用的是同一个锁,谁先拿到谁执行!
public synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
//这里没有锁!不是同步方法,不收锁的影响
public void hello(){
System.out.println("hello");
}
}
输出结果:打电话,发短信
都是静态方法了,测试一下
package com.lock8;
import java.util.concurrent.TimeUnit;
/**
* 5.增加两个静态的同步方法,只有一个对象,先答应 发短信?打电话? 先发短信
*
*/
public class Test3 {
public static void main(String[] args) {
//两个对象class 类模板只有一个,static,锁的是Class
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
//phone.sendSms; 不能回答sendSms 是先调用,锁的问题
new Thread(()->{ phone1.sendSms(); },"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// new Thread(()->{ phone.call(); },"B").start();
new Thread(()->{phone2.call();},"B").start();
}
}
// Phone3 只有唯一的一个class 对象
class Phone3 {
//synchronized 锁的对象是方法的调用者!
//static 静态方法
//类一加载就有了!锁的是 Class 模板
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call() {
System.out.println("打电话");
}
//这里没有锁!不是同步方法,不收锁的影响
public void hello()以上是关于生产者和消费者及8锁现象的主要内容,如果未能解决你的问题,请参考以下文章