zoukankan      html  css  js  c++  java
  • 多线程学习——消费者生产者(2)

    package com.daicy.ProducerConsumer;
    
    import java.util.concurrent.LinkedBlockingQueue;
    
    public class DLinkedBlockingQueue<E> implements IDStack<E> {
    
    	private LinkedBlockingQueue stack = new LinkedBlockingQueue();
    
    	@Override
    	public E push(E item) throws InterruptedException {
    		// TODO Auto-generated method stub
    		stack.put(item);
    		return item;
    	}
    
    	@Override
    	public E pop() throws InterruptedException {
    		// TODO Auto-generated method stub
    		return (E) stack.take();
    	}
    
    }
    
    package com.daicy.ProducerConsumer;
    
    import java.util.Stack;
    
    public class DStack<E> implements IDStack<E> {
    	private Stack stack = new Stack();
    
    	public synchronized E push(E item) {
    		stack.push(item);
    		notify();
    		return item;
    	}
    
    	public synchronized E pop() throws InterruptedException {
    		while (stack.size() == 0) {
    			this.wait();
    		}
    		return (E) stack.pop();
    	}
    
    }
    

    package com.daicy.ProducerConsumer;
    
    public interface IDStack<E> {
    
    	public E push(E item) throws InterruptedException;
    
    	public E pop() throws InterruptedException;
    
    }
    

    package com.daicy.ProducerConsumer;
    
    public class Mantou {
    	private int id;
    
    	Mantou(int id) {
    		this.id = id;
    	}
    
    	public String toString() {
    		return "Mantou :" + id;
    	}
    }
    

    package com.daicy.sequence;
    
    public class SafeSequence {
    	private static int value;
    
    	public static synchronized int getNext() {
    		return value++;
    	}
    }
    


  • 相关阅读:
    痛苦之旅——安装Eric4
    如何把自己写的python程序给别人用
    (转)史上最好的Python线程指南
    (转)python编码问题
    Beautiful Soup的一些中文资料
    oracle监听配置
    redhat6.5安装oracle 11g
    《深入浅出MFC》– Document-View深入探讨
    CAS解扰小结
    ts包、表、子表、section的关系
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959313.html
Copyright © 2011-2022 走看看