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

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