1. 程式人生 > >生產者和消費者模式

生產者和消費者模式

一、建立一個生產者

package consumerAndProducer;

/**
 * @author tanhw119214
 * @version JDK1.8.0_171
 * @date on  2018/8/2 16:57
 */
public class Producer implements Runnable{
    private DataStore m_data;
    public Producer(DataStore d) {
        m_data = d;
    }

    public void run() {
        System.out.print("producer thread run\n");
        int	 inPutData = 0;
        while (true) {
            m_data.producerData(inPutData++);
        }
    }
}

二、建立個消費者

package consumerAndProducer;

/**
 * @author tanhw119214
 * @version JDK1.8.0_171
 * @date on  2018`/8/2 16:56`
 */
public class Consumer implements Runnable{
    private DataStore m_data;
    public Consumer(DataStore d) {
        m_data = d;
    }

    public void run() {
        System.out.print("consumer thread run\n");
        while (true) {
            m_data.consumerData();
        }
    }
}


這裡是引用 1232142