生产者,消费者样例代码

临时写了个这个,没事的时候,可以看一看。java 写的,源码如下:

-------------------------------------------------------------------------------------------

package com.spring.test.th;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class ProduceConsumer {

    public static void main(String[] args) {
        MyShell shell = new MyShell(10);
        
        Producer pd = new Producer(shell);
        
        Consumer cs = new Consumer(shell);
        
        System.out.println("=====================5-1- running!");
        pd.start();
        cs.start();
        
        int testTime = 1000; // ms
        try {
            Thread.sleep(testTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("上帝醒了,准备终止竞赛!");
        pd.stop();
        cs.stop();
        System.out.println("竞赛停止!");
    }
}


class MyShell {
    private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.SSS";

    int leftNum = 0;
    private int fMAX_NUM = 10;
    private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);

    public MyShell(int maxLimit) {
        this.fMAX_NUM = maxLimit;
    }

    void add(String idTag) { // idTag is default set as class name.
        //System.out.println("add, idTag is " + idTag);
        if ("com.spring.test.th.Producer".equals(idTag)) {
            // Goes down
        } else {
            return;
        }

        leftNum++;
        System.out.println("after Producer add(), left " + leftNum
                + ", time " + dateFmt.format(Calendar.getInstance().getTime()));
    }
    
    void use(String idTag) {
        //System.out.println("use, idTag is " + idTag);
        if ("com.spring.test.th.Consumer".equals(idTag)) {
            // Goes down
        } else {
            return;
        }
        leftNum--;
        System.out.println("after Consumer use(), left " + leftNum
                + ", time " + dateFmt.format(Calendar.getInstance().getTime()));
    }
    
    boolean isEmpty() {
        if (leftNum < 0) {
            throw new RuntimeException("Bad operation !!");
        }
        return leftNum == 0;
    }
    
    boolean isFull() {
        if (leftNum >= fMAX_NUM) {
            System.err.println("left num is over the max num = " + leftNum);
            return true;
        }
        return false;
    }
}

class Producer extends Thread {
    private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.sssss";

    private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);

    private MyShell cmShell;
    public Producer(MyShell shell) {
        cmShell = shell;
    }

    @Override
    public void run() {
        super.run();
        System.out.println("[Producer] run() tid = " + this.getId()
            + ", date = " + dateFmt.format(Calendar.getInstance().getTime()));
        System.out.println(this.getId() + ", cmShell is " + cmShell);

        synchronized (cmShell) {
            while(true) {
                while (cmShell.isFull()) {
                    cmShell.notify(); // optional
                    System.out.println("Before produce, wait at :" + dateFmt.format(Calendar.getInstance().getTime()));
                    try {
                        cmShell.wait(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Currently time is " + dateFmt.format(Calendar.getInstance().getTime()));
                }

                cmShell.add(Producer.class.getName());
                //cmShell.notify(); // note.

                try {
                    cmShell.wait(80);
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class Consumer extends Thread {
    private static final String simpleFmt = "YYYY-MM-dd hh:mm:ss.sssss";

    private SimpleDateFormat dateFmt = new SimpleDateFormat(simpleFmt);

    private MyShell cmShell;
    public Consumer(MyShell shell) {
        cmShell = shell;
    }
    
    @Override
    public void run() {
        super.run();
        System.out.println("[Consumer] run() tid = " + this.getId()
            + ", date = " + dateFmt.format(Calendar.getInstance().getTime()));
        
        System.out.println(this.getId() + ", cmShell is " + cmShell);
        
        synchronized (cmShell) {
            while(true) {
                while (cmShell.isEmpty()) {
                    cmShell.notify(); // optional
                    System.out.println("[Consumer] Before usage, wait at :" + dateFmt.format(Calendar.getInstance().getTime()));
                    try {
                        cmShell.wait(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("[Consumer] Currently time is " + dateFmt.format(Calendar.getInstance().getTime()));
                }

                cmShell.use(Consumer.class.getName());
                //cmShell.notify(); // note.

                try {
                    cmShell.wait(100);
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

// Code end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值