java实现王道多线程情景的编写

本文展示了Java中使用Semaphore实现的生产者消费者问题解决方案,以及多生产者多消费者模型。同时,讨论了读写分离场景下的公平写法,防止写线程饥饿,并提供了两种不同的读写锁实现来确保并发控制的公平性。
摘要由CSDN通过智能技术生成

#java实现王道多线程情景的编写

生产者消费者

在这里插入图片描述


public class 生产者消费者 {
    public static void main(String[] args) {
        Semaphore full = new Semaphore(0);//表示 empty();
        Semaphore empty = new Semaphore(3);//表示 empty();
        Deque<Integer> dq = new LinkedList<>();
        Object lock=new Object();
        new Thread(new Runnable(){
            //生产者代码
            public void run(){
                while(true){
                    try {
                        empty.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock){
                        dq.offerFirst(1);
                        System.out.println("放入了一个资源");
                    }
                    full.release();
                }
            }
        }).start();
        new Thread(new Runnable() {
            //这里是消费者代码
            @SneakyThrows
            @Override
            public void run() {
                while (true){
                    try {
                        full.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock){
                        Thread.sleep(500);
                        Integer first = dq.pollFirst();//直接取,保证有;
                        System.out.println(first);
                    }
                    empty.release();
                }
            }
        }).start();
    }
}


多生产者多消费者

在这里插入图片描述


public class 多消费者模型 {


    public static void main(String[] args) {


        Semaphore plate = new Semaphore(1);

        Semaphore orange=new Semaphore(0);
        Semaphore apple=new Semaphore(0);
        Thread dad = new Thread(new Runnable() {
            //dad、、 往里面放橙子
            @SneakyThrows
            @Override
            public void run() {
                while(true){
                    Thread.sleep(300);
                    try {
                        plate.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    orange.release();//女儿爱吃橘子
                }

            }
        });
        Thread mom = new Thread(new Runnable() {
            //mom 往里面放apple
            @SneakyThrows
            @Override
            public void run() {
                while(true){
                    Thread.sleep(1000);//苹果生产速度很慢
                    try {
                        plate.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    apple.release();//儿子爱吃苹果
                }
            }
        });
        Thread son = new Thread(new Runnable() {
            //dad、、 往里面放橙子
            @Override
            public void run() {
                while(true){
                    try {
                        apple.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("儿子吃到了一个苹果");
                    plate.release();
                }

            }
        });
        Thread daughter = new Thread(new Runnable() {
            //dad、、 往里面放橙子
            @Override
            public void run() {
                while (true){
                    try {
                        orange.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("女儿吃到了一个橘子");
                    plate.release();
                }

            }
        });
        dad.start();
        mom.start();
        son.start();
        daughter.start();
    }
}

读写分离,饥饿的情况

在这里插入图片描述


public class 读写锁公平写法 {
    static  int count=0;
    public static void main(String[] args) {
        Lock mu =new ReentrantLock();
        Lock rw=new ReentrantLock();
        new Thread(()->{
            while(true){
                try{
                    rw.lock();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我是写线程");
                }catch (Exception e){
                }
                finally {
                    rw.unlock();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
       new Thread(()->{
           while (true){
               mu.lock();
               count++;
               if(count==1){
                   rw.lock();
               }
               mu.unlock();
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("我在读1");
               mu.lock();
               count--;
               if(count==0){
                   rw.unlock();
               }
               mu.unlock();
           }
       }).start();
       //读的线程是可以并发跑起来的;
        new Thread(()->{
            while (true){
                mu.lock();
                count++;
                if(count==1){
                    rw.lock();
                }
                mu.unlock();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("我在读2");
                mu.lock();
                count--;
                if(count==0){
                    rw.unlock();
                }
                mu.unlock();
            }
        }).start();

    }
}

读写分离 保护写不会被饥饿

在这里插入图片描述


public class 读写锁公平写法2 {
    static  int count=0;
    public static void main(String[] args) {
        Lock mu =new ReentrantLock();
        Lock rw=new ReentrantLock();
        Lock r3=new ReentrantLock();
       new Thread(()->{
           while (true){
               try{
                   r3.lock();
                   mu.lock();
                   if(count==0){
                       rw.lock();
                   }
                   count++;
               }catch (Exception e){}
               finally {
                   mu.unlock();
                   r3.unlock();
               }
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("我在读1");
               try{
                   mu.lock();
                   count--;
                   if(count==0){
                       rw.unlock();
                   }
               }catch (Exception e){}
               finally {
                   mu.unlock();
               }

           }
       },"读者1").start();
       //读的线程是可以并发跑起来的;
        new Thread(()->{
            while (true){
                try{
                    r3.lock();
                    mu.lock();
                    if(count==0){
                        rw.lock();
                    }
                    count++;
                }catch (Exception e){}
                finally {
                    mu.unlock();
                    r3.unlock();
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("我在读2");
                try{
                    mu.lock();
                    count--;
                    if(count==0){
                        rw.unlock();
                    }
                }catch (Exception e){}
                finally {
                    mu.unlock();
                }
            }
        },"读者2").start();
        new Thread(()->{
            while(true){
                try{
                    r3.lock();
                    rw.lock();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("我是写线程");
                }catch (Exception e){
                }
                finally {
                    rw.unlock();
                    r3.unlock();
                }
            }
        }).start();
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值