现在的位置: 首页 > 综合 > 正文

java 线程+栈 生产者消费者关系

2017年11月08日 ⁄ 综合 ⁄ 共 2659字 ⁄ 字号 评论关闭

package practice.produce;

public class Producer implements Runnable{
    ProStack ps;
    int n;
    public Producer(){}
    public Producer(ProStack ps){
        this.ps = ps;
    }
    public void run(){
        //因只是模拟 所以设置总共生产100次,若用while(true)循环,可使其无限生产
        for(int i = 0 ;i < 100; i++){
            ps.pushOne(n);
            n++;
        }
    }

}

package practice.produce;

public class Consumer implements Runnable{
    ProStack ps;
   
    public Consumer(){}
    public Consumer(ProStack ps){
        this.ps = ps;
    }
    public void run() {
        while(true){
            ps.popOne();
        }   
    }
}

package practice.produce;

import java.util.Stack;

public class ProStack {
    Stack<Integer> stack = new Stack<Integer>();
    int max = 5; //保存产品上限
    public void pushOne(int n){
        synchronized(this){
   
            while(stack.size() == max){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
           
            System.out.println("--produce:"+stack.push(n));
           
            //小睡一会,使结果明显一点
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
            notifyAll();
        }
    }
    public void popOne(){
        synchronized(this){
            while(stack.isEmpty()){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
           
            System.out.println("consume:"+stack.pop());
           
            //小睡一会,使结果明显一点
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
            notifyAll();
        }
    }
}

package practice.produce;

public class Test {
    //本程序利用线程简单实现生产者与消费者的生产消费关系,利用栈来保存生产的产品
    //产品上限为 5
    public static void main(String[] args) {
        ProStack ps = new ProStack();
        Producer produce = new Producer(ps);
        Consumer consume = new Consumer(ps);
       
        Thread t1 = new Thread(produce);
        Thread t2 = new Thread(consume);

        t1.start();
        t2.start();
    }
}

/**
* part of one result shows:
* (
*   if it's hard to get a obvious result
*   try to modify "Thread.sleep(100);" to "Thread.sleep(500);"
* )
*
--produce:0
--produce:1
--produce:2
--produce:3
--produce:4
consume:4
consume:3
consume:2
--produce:5
--produce:6
--produce:7
consume:7
--produce:8
consume:8
consume:6
consume:5
--produce:9
--produce:10
--produce:11
consume:11
--produce:12
consume:12
--produce:13
consume:13
consume:10
consume:9
consume:1
consume:0
--produce:14
--produce:15
consume:15
consume:14
--produce:16
--produce:17
--produce:18
--produce:19
--produce:20
consume:20
consume:19
consume:18
consume:17
consume:16
...
*
*/

人家的代码,贴出来

抱歉!评论已关闭.