码迷,mamicode.com
首页 > 其他好文 > 详细

生产者-消费者模式

时间:2016-10-07 13:40:46      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

  • 生产者负责生产产品。
  • 消费者负责取走并使用产品。
  • 生产者生产完成后通知消费者可以取走产品了。
  • 消费者消费完产品后需要通知生产者生产产品。
  • 生产者没有生产完成,消费者不能取走产品。
  • 消费者没有使用完产品,生产者不能生产产品。
package main;

import java.util.*;

class TicketSeller implements Runnable
{
    public static Integer ticketSoldTotal = 0;
    private Integer totalTicket = 300;
    private Random random = new Random();
    
    @Override
    public void run()
    {
        for(int i = 0; i < 500; i++)
        {
            synchronized(this)
            {
                if(this.totalTicket > 0)
                {
                    Util.sleep(500);
                    int ticketSold = random.nextInt(5);
                    ticketSold++;
                    if((this.totalTicket - ticketSold) < 0)
                    {
                        ticketSold = this.totalTicket;
                    }
                    ticketSoldTotal += ticketSold;
                    this.totalTicket -= ticketSold;
                    System.out.println(Thread.currentThread().getName()+" sells "+ticketSold+" ticket(s), " + this.totalTicket + " left. ");
                }
            }
        }
    }
}

class Util
{
    public static void sleep(Integer sleepTime)
    {
        try
        {
            Thread.sleep(sleepTime);
        }
        catch(Exception e)
        {}
    }
}

class Resource
{
    private String name;
    public Resource(String name)
    {
        this.name = name;
    }
    
    public String getName()
    {
        return this.name;
    }
}

class WritePaper implements Runnable
{
    private Resource resourceA;
    private Resource resourceB;
    
    
    
    public WritePaper(Resource resourceA, Resource resourceB)
    {
        super();
        this.resourceA = resourceA;
        this.resourceB = resourceB;
    }



    public void run()
    {
        synchronized(this.resourceA)
        {
            Util.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "" + this.resourceA.getName());
            synchronized(resourceB)
            {
                System.out.println(Thread.currentThread().getName() + "" + this.resourceB.getName());
            }
        }
    }
}

class Product
{
    private String name;
    private String desc;
    private Boolean readyToGet = false;
    
    public Product()
    {
        super();
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getDesc()
    {
        return desc;
    }
    public void setDesc(String desc)
    {
        this.desc = desc;
    }
    
    public synchronized void setInfo(String name, String desc)
    {
        if(this.readyToGet == true)
        {
            try
            {
                super.wait();
            }
            catch(Exception e)
            {}
        }
        
        
        this.setName(name);
        Util.sleep(400);
        this.setDesc(desc);
        
        this.readyToGet = true;
        super.notifyAll();
    }
    
    public synchronized String getInfo()
    {
        if(this.readyToGet == false)
        {
            try
            {
                super.wait();
            }
            catch(Exception e)
            {}
        }
        
        String name = this.getName();
        Util.sleep(300);
        String desc = this.getDesc();
        
        this.readyToGet = false;
        super.notifyAll();
        return "Name: " + name + " Desc: " + desc;
    }
    
    @Override
    public String toString()
    {
        return "Product [name=" + name + ", desc=" + desc + "]";
    }
    
    
}

class Produce implements Runnable
{
    private Product product;
    
    public Produce(Product product)
    {
        super();
        this.product = product;
    }

    @Override
    public void run()
    {
        for(int i = 0; i < 30; i++)
        {
            if(i%2 == 0)
                this.product.setInfo("fan", "Thre gorges");
            else
                this.product.setInfo("computer", "HP");
        }
    }
}

class Comsume implements Runnable
{
    private Product product;
    
    public Comsume(Product product)
    {
        super();
        this.product = product;
    }

    @Override
    public void run()
    {
        for(int i = 0; i < 30; i++)
        {
            System.out.println(this.product.getInfo());
        }
        
    }
    
}


public class main
{
    public static void main(String[] args) throws Exception
    {
        Product product = new Product();
        Comsume c = new Comsume(product);
        Produce p = new Produce(product);
        
        List<Thread> threadList = new ArrayList<Thread>();
        threadList.add(new Thread(p));
        threadList.add(new Thread(c));
        
        for(Thread thread : threadList)
            thread.start();
        
        for(Thread thread : threadList)
            thread.join();
        
        System.out.println("///~Main done");
    }
    
    public static List<Integer> getDemoList()
    {
        Random random = new Random();
        List<Integer> list = new ArrayList<>();
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        list.add(random.nextInt(30));
        
        return list;
    }
}

 

生产者-消费者模式

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5935739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!