Java 實現的生產者消費者小例

package mypackage;
import java.util.*;
public class iphxer
{
    public static void main(String s[])
    {
        System.out.println("開始生產和消費了!");
        M m = new M();
        m.start();
    }
}
class M extends Thread
{
    public int MAX;
    public Queue queue = new LinkedList();
    public M()
    {
        this.MAX=10;
    }
    public void run()
    {
        produce p = new produce(5000);
        p.start();
        consumer c = new consumer(1000);
        c.start();
    }
    public boolean isempty()
    {
        return this.size() == 0;
    }
    public boolean isfull()
    {
        return this.MAX <= this.size();
    }
    public void push_back(String s)
    {
        this.queue.offer(s);
    }
    public String pop_front()
    {
        return this.queue.poll();
    }
    public  int size()
    {
        return this.queue.size();
    }
    
    class produce extends Thread
    {
        private int num;//生產數量
        private int time;//生產速度
        public produce(int time)
        {
            this.time = time;
            this.num = 0;
            System.out.println("生產者對象已經構造出來了……");
        }
        public void run()
        {
            System.out.println("生產者開始生產了!");
            while(true)
            {
                try
                {
                    while(!M.this.isfull())
                    {
                        System.out.println("生產第" + (M.this.size() + 1) + "個……總第" + this.num + "個 ");
                        Thread.sleep(this.time);//表示生產的速度;
                        M.this.push_back("產品" + M.this.size());
                        // System.out.println("成功生產第" + (M.this.size() + 1) + "個……");
                        ++this.num;
                        Thread.sleep(500);
                    }
                }
                catch(Exception e)
                {
                    e.setStackTrace(null);
                }
                System.out.println("生產滿了,正在等待……");
                try
                {
                    Thread.sleep(1000);
                }
                catch(Exception e)
                {
                    e.setStackTrace(null);
                }
            } 
        }
    }
    
    class consumer extends Thread
    {
        private int num;//消費數量
        private int time;//消費速度
        public consumer(int time)
        {
            this.time = time;
            this.num = 0;
        }
        public void run()
        {
            String s;
            while(true)
            {
                while(!M.this.isempty())
                {
                    try
                    {
                        System.out.println("消費第" + (M.this.size()) + "個……總第" + (this.num + 1) + "個 ");
                        Thread.sleep(this.time);//表示生產的速度;
                        s = M.this.pop_front();
                        //System.out.println("成功消費" + s + "……");
                        ++this.num;
                        Thread.sleep(500);
                    }
                    catch(Exception e)
                    {
                        e.setStackTrace(null);
                    }
                }
                System.out.println(" 產品空了,正在等待生產……");
                try
                {
                    Thread.sleep(1000);
                }
                catch(Exception e)
                {
                    e.setStackTrace(null);
                }
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章