ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JUC并发编程(二)——线程间通信和定制化通信

2021-12-23 06:02:01  阅读:121  来源: 互联网

标签:JUC int lock number private 间通信 线程 public


线程间通信和定制化通信

线程间通信概述

线程间通信的模型有两种:共享内存和消息传递,以下方式都是基本这两种模型来实现的。

模拟场景---两个线程,一个线程对当前数值加 1,另一个线程对当前数值减 1,要求用线程间通信

synchronized 方案

//创建资源类,定义属性和操作方法
class Share{
    
    private int number = 0;
    
    //+1方法
    public synchronized void incr() throws InterruptedException {
        //二、判断 操作和通知
        while (number != 0) this.wait();//判断number值是否为0
        //如果number值为0
        number++;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        //通知其他线程
        this.notifyAll();
    }
    //-1方法
    public synchronized void decr() throws InterruptedException {
        //判断
        while (number != 1) this.wait();
        //操作
        number--;
        System.out.println(Thread.currentThread().getName()+"::"+number);
        //通知其他线程
        this.notifyAll();
    }
}
public class ThreadDemo1 {
    //创建多个线程,调用资源类的操作方法
    public static void main(String[] args) {
        Share share = new Share();
        //创建线程
        new Thread(()->{
            for (int i = 1;i <= 10;i++){
                try {
                    share.incr(); //进行10次+1操作
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程一").start();

        new Thread(()->{
            for (int i = 1;i <= 10;i++){
                try {
                    share.decr(); //进行10次-1操作
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程二").start();
    }
}

Lock 方案

//第一步,创建资源类,定义属性和操作方法
class Share{
    private int number = 0;
    //创建lock
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    //+1
    public void incr() throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断
            while (number != 0) condition.await();
            //操作
            number++;
            System.out.println(Thread.currentThread().getName()+"::"+number);
            //通知
            condition.signalAll();
        }finally {
            //解锁
            lock.unlock();
        }
    }

    //-1
    public void decr() throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断
            while (number !=1) condition.await();
            //操作
            number--;
            System.out.println(Thread.currentThread().getName()+"::"+number);
            //通知
            condition.signalAll();
        }finally {
            //解锁
            lock.unlock();
        }
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) {
        Share share = new Share();

        //创建多个线程
        new Thread(()->{
            for (int i=1;i<=10;i++){
                try {
                    share.incr();//进行10次+1操作
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程一").start();

        new Thread(()->{
            for (int i=1;i<=10;i++){
                try {
                    share.decr();//进行10次-1操作
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程二").start();
    }
}

线程间定制化通信

案例:线程一打印 5 次,线程二打印 10 次,线程三打印 15 次 ,按照此顺序循环 10 轮

//创建资源类,定义属性和操作方法
class ShareResource{
    //定义标志位
    private int flag = 1;
    //创建Lock锁
    private Lock lock = new ReentrantLock();

    //创建三个condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    //打印5次,参数 第几轮
    public void print5(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断
            while (flag != 1){
                c1.await();
            }
            //操作 打印5次
            for (int i = 1;i <= 5;i++) System.out.println(Thread.currentThread().getName()+"::"+i+":轮数"+loop);
            //通知 先修改标志位为2 再通知第二个线程
            flag = 2;
            c2.signal();
        }finally {
            lock.unlock();
        }
    }
    public void print10(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断
            while (flag !=2){
                c2.await();
            }
            //操作 打印10次
            for (int i = 1; i <= 10; i++) System.out.println(Thread.currentThread().getName()+"::"+i+":轮数"+loop);
            flag = 3;
            //通知
            c3.signal();
        }finally {
            lock.unlock();
        }
    }

    public void print15(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断
            while (flag != 3){
                c3.await();
            }
            //操作 打印15次
            for (int i = 1; i <= 15; i++) System.out.println(Thread.currentThread().getName()+"::"+i+":轮数"+loop);
            flag = 1;
            //通知
            c1.signal();
        }finally {
            lock.unlock();
        }
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print5(i); //调用打印5次的方法
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程一").start();
        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print10(i); //调用打印10次的方法
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程二").start();
        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print15(i);//调用打印15次的方法
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程三").start();
    }
}

标签:JUC,int,lock,number,private,间通信,线程,public
来源: https://www.cnblogs.com/luoxiao1104/p/15721838.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有