栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java阻塞队列BlockingQueue

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java阻塞队列BlockingQueue

1.什么是阻塞队列?

 

2.为什么用,有什么好处?

3.BlockingQueue的核心方法

 3.1抛出异常组 

package com.queue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;


public class BlockingQueueDemo {
    public static void main(String[] args) {

      //  List list= new ArrayList<>();
        BlockingQueue blockingQueue =new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

         

        System.out.println(blockingQueue.add("d"));

       

        
        System.out.println(blockingQueue.element());


        
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());


    }
}
3.2 特殊值

package com.queue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;


public class BlockingQueueDemo {
    public static void main(String[] args) {

      //  List list= new ArrayList<>();
        BlockingQueue blockingQueue =new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));

        

        System.out.println(blockingQueue.offer("z"));

        

        System.out.println(blockingQueue.peek());

        

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());


    }
}
3.3 阻塞

package com.queue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;


public class BlockingQueueDemo {
    public static void main(String[] args) throws Exception{

      //  List list= new ArrayList<>();
        BlockingQueue blockingQueue =new ArrayBlockingQueue(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println("======================================");

        
        blockingQueue.put("d");

        blockingQueue.take();
        blockingQueue.take();
        blockingQueue.take();

        
        blockingQueue.take();



    }
}
3.4 超时

package com.queue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;


public class BlockingQueueDemo {
    public static void main(String[] args) throws Exception{

      //  List list= new ArrayList<>();
        BlockingQueue blockingQueue =new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.offer("a", 2L, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b", 2L, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c", 2L, TimeUnit.SECONDS));

        
        System.out.println(blockingQueue.offer("d", 2L, TimeUnit.SECONDS));
    }
}

4. SynchronousQueue

不存储元素的阻塞队列,也即单个元素的对列

package com.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;



public class SynchronousQueueDemo {

    public static void main(String[] args) {

        BlockingQueue blockingQueue = new SynchronousQueue<>();

        new Thread(() -> {
            try {

                System.out.println(Thread.currentThread().getName() + "t put 1");
                blockingQueue.put("1");

                System.out.println(Thread.currentThread().getName() + "t put 2");
                blockingQueue.put("2");

                System.out.println(Thread.currentThread().getName() + "t put 3");
                blockingQueue.put("3");


            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "AAA").start();


        new Thread(() -> {
            try {

                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+"t "+ blockingQueue.take());

                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+"t "+ blockingQueue.take());

                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName()+"t "+ blockingQueue.take());


            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "BBB").start();

    }
}

结果输出:

AAA     put 1
BBB     1
AAA     put 2
BBB     2
AAA     put 3
BBB     3

5.用途,应在在哪里? 5.1生产者消费者模式
package com.thread;


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

class ShareData {

    private int number = 0;
    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public void increment() throws Exception {
        lock.lock();
        try {
            //判断 一定要用while 防止虚假唤醒
            while (number != 0) {

                //等待不能生产
                condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "t" + number);
            //通知唤醒
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() throws Exception {
        lock.lock();
        try {
            //判断
            while (number == 0) {

                //等待不能生产
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "t" + number);
            //通知唤醒
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


}


public class ProdConstumerDemo {

    public static void main(String[] args) {

        ShareData shareData = new ShareData();
        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                try {
                    shareData.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 1; i <= 5; i++) {
                try {
                    shareData.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "BB").start();


        //=======================================



//        new Thread(() -> {
//            for (int i = 1; i <= 5; i++) {
//                try {
//                    shareData.increment();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }, "CC").start();
//
//        new Thread(() -> {
//            for (int i = 1; i <= 5; i++) {
//                try {
//                    shareData.decrement();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }, "DD").start();



    }
}

输出:

AA    1
BB    0
AA    1
BB    0
AA    1
BB    0
AA    1
BB    0
AA    1
BB    0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号