原创 

java一对一生产者消费者多线程练习

分类:    655人阅读    IT小君  2017-04-24 10:36

消费者OTO_Consumer类:


/**
 * Created by YouGuessWho on 2017/4/22.
 */
public class OTO_Consumer extends Thread {
    /**
     * 编号
     */
    private Integer num;
    private Object lock;

    public OTO_Consumer(String name,Integer num,Object lock){
        super();
        this.setName(name);
        this.setNum(num);
        this.setLock(lock);
    }

    public void run(){
        try {
            while(true) {
                this.consume();
                this.sleep(3000);
            }
        }catch(final Exception ex){
            System.out.println(ex);
        }
    }

    private void consume()throws Exception{
        synchronized(this.lock){
            if(Warehouse.simpleWH==null){
                this.say("没货了");
                lock.wait();
            }
            this.say("消费了"+Warehouse.simpleWH);
            Warehouse.simpleWH = null;
            lock.notify();
        }
    }

    private void say(String word){
        System.out.println(String.format("时间:%s,%s,编号%03d:%s",System.currentTimeMillis(),this.getName(),this.getNum(),word));
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Object getLock() {
        return lock;
    }

    public void setLock(Object lock) {
        this.lock = lock;
    }
}


生产者():


/**
 * Created by YouGuessWho on 2017/4/22.
 */
public class OTO_Producer extends Thread {
    /**
     * 编号
     */
    private Integer num;
    private Object lock;

    public OTO_Producer(String name,Integer num,Object lock){
        super();
        this.setName(name);
        this.setNum(num);
        this.setLock(lock);
    }

    public void run(){
        try {
            while(true) {
                this.produce();
                this.sleep(3000);
            }
        }catch(final Exception ex){
            System.out.println(ex);
        }
    }

    private void produce()throws Exception{
        synchronized(this.lock){
            if(Warehouse.simpleWH!=null){
                this.say("还有产品");
                lock.wait();
            }
            Warehouse.simpleWH = "产品"+System.currentTimeMillis();
            this.say("生产了"+Warehouse.simpleWH);
            lock.notify();
        }
    }

    private void say(String word){
        System.out.println(String.format("时间:%s,%s,编号%03d:%s",System.currentTimeMillis(),this.getName(),this.getNum(),word));
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Object getLock() {
        return lock;
    }

    public void setLock(Object lock) {
        this.lock = lock;
    }
}

启动类(Producer_Consumer_OneToOne);


/**
 * Created by YouGuessWho on 2017/4/22.
 */
public class Producer_Consumer_OneToOne {

    public static void main(String[] arg){
        Object lock = new Object();
        OTO_Producer producer = new OTO_Producer("简单生产者",1,lock);
        OTO_Consumer consumer = new OTO_Consumer("简单消费者",1,lock);
        producer.start();
        consumer.start();
    }
}


支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者

 工具推荐 更多»