JUC- 线程间通信

12 篇文章 0 订阅

线程间通信

线程间通信的模型有两种:共享内存消息传递,以下方式都是基本这两种模型来实现的。
我们来基本一道面试常见的题目来分析
场景

两个线程,一个线程对当前数值加 1,另一个线程对当前数值减 1,
要求:用线程间通信

synchronized 方案

package com.java.juc.sync;
/**
 * 第一步 创建资源类,定义属性和操作方法
 * @author chejuan
 */
class Share{
    /**
     * 初始值
     */
    private int number = 0;
    /**
     * +1的方法
     */
    public synchronized void incr() throws InterruptedException {
        //第二步 判断 干活 通知
        //number != 0,则等待;number = 0,+1;
        while( number != 0 ){
            this.wait();
        }
        number++;
        System.out.println("incr===" + Thread.currentThread().getName() + " :: " + number);
        //通知其他线程
        this.notifyAll();
    }

    /**
     * -1的方法
     */
    public synchronized void decr() throws InterruptedException {
        //第二步 判断 干活 通知
        //number != 1,则等待 ; number != 0,-1
       //if( number != 1 ){
       //     this.wait();//在哪里睡,再哪里醒  会存在虚假唤醒的问题
       // }
        while( number != 1 ){
            this.wait();//while循环是不管在哪里睡,都需要判断
        }
        number--;
        System.out.println("decr===" + Thread.currentThread().getName() + " :: " + number);
        //通知其他线程
        this.notifyAll();

    }
}

/**
 * 第三步 创建多个线程,操作资源类的方法
 * @author chejuan
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        Share share = new Share();
        //创建线程
        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.incr();//+1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.decr();//-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.incr();//+1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"CC").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.decr();//-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"DD").start();
    }
}

Lock 方案

package com.java.juc.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 第一步 创建资源类,定义属性和操作方法
 * @author chejuan
 */
class Share{
    /**
     * 初始值
     */
    private int number = 0;

    /**
     * 创建可重入锁
     */
    private Lock lock = new ReentrantLock();

    /**
     * 创建
     */
    private Condition condition = lock.newCondition();

    /**
     * +1的方法
     */
    public void incr() throws InterruptedException {
        //上锁
        lock.lock();
        try{
            //第二步 判断 干活 通知
            //number != 0,则等待;number = 0,+1;
            while( number != 0 ){
                condition.await();
            }
            number++;
            System.out.println("incr===" + Thread.currentThread().getName() + " :: " + number);
            //通知其他线程
            condition.signalAll();

        } finally {
            //解锁
            lock.unlock();
        }
    }

    /**
     * -1的方法
     */
    public void decr() throws InterruptedException {
        //上锁
        lock.lock();
        try{
            //第二步 判断 干活 通知
            //number != 1,则等待 ; number != 0,-1
            while( number != 1 ){
                condition.await();//while循环是不管在哪里睡,都需要判断
            }
            number--;
            System.out.println("decr===" + Thread.currentThread().getName() + " :: " + number);
            //通知其他线程
            condition.signalAll();
        } finally {
            //解锁
            lock.unlock();
        }
    }
}


/**
 * 第三步 创建多个线程,操作资源类的方法
 * @author chejuan
 */
public class ThreadDemo2 {
    public static void main(String[] args) {
        Share share = new Share();
        //创建线程
        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.incr();//+1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.decr();//-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.incr();//+1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"CC").start();

        new Thread(()->{
            for(int i = 0; i < 10; i++){
                try {
                    share.decr();//-1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"DD").start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值