java阻塞队列学习

生产者线程消费者线程提交到阻塞队列;线程使用线程池技术管理,

https://www.cnblogs.com/superfj/p/7757876.html 感觉最后程序需要改进,至少消费者不知道有多少任务

//修改阻塞队列大小,修改线程大小;线程填充数据,搬运数据;队列可以满或者是不满;但是没有出现异常或者丢失任务
public class ZuSeQue {
    public static void main(String[] args) {
        /*MyBlockQue que=new MyBlockQue(null);
        System.out.println(que.hasNext());*/
        ExecutorService executors=Executors.newFixedThreadPool(2);
        executors.submit(new Provider("你好"));
        executors.submit(new Provider("是谁"));
        executors.submit(new Provider("溢出"));
      //  executors.submit(new Customer());
        MyBlockQue myBlockQue=new MyBlockQue();
        Thread.sleep(1000);//主线程等待,不然队列中可能没有内容
        while(myBlockQue.hasNext()){
            Future future=executors.submit(new CustoReturn());//感谢java并发编程手册阿里巴巴
            Object obj=future.get();
            System.out.println(obj.toString()+"-cus");    
        }
        executors.shutdown();
    }
}
class CustoReturn implements Callable<Object>{
    
    @Override
    public Object call() throws Exception {
        MyBlockQue myBlockQue=new MyBlockQue();
        
        if(myBlockQue.hasNext()){
            System.out.println(myBlockQue.hasCapacity());            
            try {
                Object ni=myBlockQue.take();
                return ni;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    
}
class Customer implements Runnable{    
    @Override
    public void run() {
        MyBlockQue myBlockQue=new MyBlockQue();
        while(myBlockQue.hasNext()){
            System.out.println(myBlockQue.hasCapacity());
            System.out.println(Thread.currentThread().getName()+"cus");
            try {
                Object ni=myBlockQue.take();
                System.out.println(ni.toString()+"-cus");

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
}
class Provider implements Runnable{
    Object object;
    public Provider(Object object) {
        System.out.println(object.toString()+"pro");
        this.object=object;
    }
    @Override
    public void run() {
        MyBlockQue myBlockQue=new MyBlockQue(object);
        System.out.println(Thread.currentThread().getName()+"pro");
        try {
            System.out.println(myBlockQue.hasCapacity());
            myBlockQue.put();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
}
class MyBlockQue{
    //无界限的
    Object object;
    public MyBlockQue(){
        
    }
    public MyBlockQue(Object obj) {
        object=obj;
    }
    public String hasCapacity(){
        return blockQue.remainingCapacity()+":ca and size:"+blockQue.size();
    }

    final static BlockingQueue<Object> blockQue =new LinkedBlockingQueue<Object>(2);
    public void put() throws InterruptedException {
        blockQue.put(object);
    }
    //队列加上线程池
    public Object take() throws InterruptedException {
        return blockQue.take();
    }
    public Boolean hasNext(){
        return blockQue.peek()==null?false:true;
    }

}

你好pro
是谁pro
溢出pro
pool-1-thread-1pro
pool-1-thread-2pro
2:ca and size:0
2:ca and size:0
pool-1-thread-1pro
0:ca and size:2
0:ca and size:2
你好-cus
0:ca and size:2
是谁-cus
1:ca and size:1
溢出-cus

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章