Java:多线程

时间:2014-11-20 13:13:07

标签: java multithreading synchronization

/ *这里出了什么问题,每次生产者只执行而消费者没有消费任何东西。请解释一下这段代码出了什么问题。

我的预期结果:生产者生产1项,然后消费者必须使用该项* /

import java.util.ArrayList;

public class ConsumerAndProducerMain
{
    public static void main(String[] args) throws InterruptedException
    {
        int MAX_SIZE = 10;
        ArrayList<Integer> list = new ArrayList<Integer>(10);
        final Producer producer = new Producer(MAX_SIZE, list);
        final Consumer consumer = new Consumer(MAX_SIZE, list);
        Thread t1 = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    producer.producer();
                }
                catch (InterruptedException e)
                {
                    System.out.println("Producer : Exception occured because of multi threading...");
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    consumer.consumer();
                }
                catch (InterruptedException e)
                {
                    System.out.println("Consumer : Exception occured because of multi threading...");
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Program terminated..");
    }
}

class Producer {
    int size;
    ArrayList<Integer> list;
    public Producer(int size, ArrayList<Integer> list)
    {
        this.size = size;
        this.list = list;
    }
    public void producer() throws InterruptedException{
        int value = 0;
        while(true){
            synchronized (this)
            {
                if(list.size() == size){
                    System.out.println("Producer : List is full");
                    wait();
                }else{
                    list.add(++value);
                    Thread.sleep(200);
                    notify();
                    System.out.println("Value produce by the producer : "+value);
                }
            }
        }
    }
}

class Consumer {
    int size;
    ArrayList<Integer> list;
    public Consumer(int size, ArrayList<Integer> list)
    {
        this.size = size;
        this.list = list;
    }
    public void consumer() throws InterruptedException{
        while(true){
            synchronized (this)
            {
                if(list.isEmpty()){
                    System.out.println("Consumer : List is empty");
                    wait();
                }else{
                    int removedItem = list.remove(list.size());
                    Thread.sleep(5000);
                    notify();
                    System.out.println("Value consumed by the consumer : "+removedItem);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,你需要sycnhronize,等待并通知你使用的列表,而不是你所做的对象Producer和Consumer。

制片:

final ArrayList<Integer> list;
// ...
synchronized (list) {
  if (list.size() == size) {
    System.out.println("Producer : List is full");
    list.wait();
  } else {
    list.add(++value);
    Thread.sleep(200);
    list.notify();
    System.out.println("Value produce by the producer : " + value);
  }
}

消费者:

final ArrayList<Integer> list;
// ...
synchronized (list) {
  if (list.isEmpty()) {
    System.out.println("Consumer : List is empty");
    list.wait();
  } else {
    int removedItem = list.remove(list.size() - 1);
    Thread.sleep(200);
    list.notify();
    System.out.println("Value consumed by the consumer : " + removedItem);
  }
}

输出如下:

Value produce by the producer : 1
Value produce by the producer : 2
Value produce by the producer : 3
Value produce by the producer : 4
Value produce by the producer : 5
Value produce by the producer : 6
Value produce by the producer : 7
Value produce by the producer : 8
Value produce by the producer : 9
Value produce by the producer : 10
Producer : List is full
Value consumed by the consumer : 10
Value consumed by the consumer : 9
Value consumed by the consumer : 8
...

对于您的预期结果(生产者生产1项,然后消费者必须使用该项)您需要更改逻辑的等待顺序并通知:生产者添加项目,通知,然后等待消费者通知,消费者等待生产者通知,消耗1项,然后再等待生产者通知。