多线程的问题(消费者和生产者) 消费者无法消费

package pack01;


import java.util.ArrayList;

import java.util.List;


/**

 * 测试缓存区

 * @author 

 *

 */

public class TestBuffer {

public static void main(String[] args) {

List<Data> list = new ArrayList<Data>();//生成一个缓存区的储存器

Buffer buffer = new Buffer(list,20);//生成一个缓存区

Consumer consumer = new Consumer(buffer);//生成一个消费者

Producer producer = new Producer(buffer);//生成一个生产者

new Thread(producer).start();//启动线程

new Thread(consumer).start();

}

}


class Consumer implements Runnable//消费者

{

private Buffer buffer;//为消费者指定缓存区

public Consumer(Buffer buffer) {

//在构造器中指定缓存区

this.buffer = buffer;

}

public synchronized void consume()//消费

{

if(this.buffer.dataBuffer.size() == 0)

{

System.out.println("缓存区中无数据可消费!");

try {

this.wait();//如果缓存区中无数据可消费 暂停该线程

} catch (InterruptedException e) {

e.printStackTrace();

}

}

this.buffer.dataBuffer.remove(this.buffer.dataBuffer.size()-1);

this.buffer.len--;//缓存区的当前大小减1

this.notifyAll();//消费完毕 通知其他线程

}

@Override

public void run() {

for(int i = 1; i < 10; i++)

{

this.consume();

System.out.println("消费第"+i+"个数据");

}

}

}


class Producer implements Runnable//生产者

{

private Buffer buffer;//为生产者指定容器

public Producer(Buffer buffer) {

this.buffer = buffer;//在构造器中指定容器

}

public synchronized void produce(Data data)//生产

{

if(this.buffer.dataBuffer.size() == this.buffer.maxSize)//如果缓存区的大小等于缓存区的最大大小

{

System.out.println("缓存区已满!");

try {

this.wait();//如果缓存区已满 就暂停该线程

} catch (InterruptedException e) {

e.printStackTrace();

}

}

this.buffer.dataBuffer.add(data);//如果不等 则往缓存区中加入data

this.buffer.len++;//缓存区的当前大小加1

this.notifyAll();//生产完毕 通知其他线程

}

@Override

public void run() {

for(int i = 1; i <= 20; i++)

{

this.produce(new Data(i));

System.out.println("生产第"+i+"个数据");

}

}

}


class Buffer//缓存区

{

List <Data> dataBuffer;//指定一个储存data的容器

int maxSize;//指定缓存区的最大大小

int len;//指定缓存区的当前大小

public Buffer(List <Data> dataBuffer, int maxSize) {

//在构造时 设置缓存区的的大小

this.dataBuffer = dataBuffer;

this.maxSize = maxSize;

this.len = this.dataBuffer.size();

}

}


class Data

{

int id;//指定id


public Data(int id) {

this.id = id;

}

}



轩莫
浏览 863回答 2
2回答

慕设计2395807

LKNMRKJWUDJCCSUGDPIBHGHTZRRHIHFRKNFJKMYUGIBQEDBRNTILYASMUJZUDDETKFEMMVEEXIYNMBQEUMFUPIUWTPEDXTVMENNVRMOYGOEGGFEJSHUGUJZBUZYWSXDPGPRQOAFUKMMLJVBBQGYSGYCRDJALLKVSHTZYKPBCUTTFSERQUHUGTFFWWWVRWIJVVWVIUHHTGOASRFSRERDUHGTFREEQJVUUUUYXXXJRDQDPUUUUTGYXJKWOAMMLYWDWCTONUBTYJVVNNAANMYKDCOAMNNAMEQNZYQCPQCCBBRQCBBAOAANMSRQOPHGFFFREDQSFFFKKKJKCBOPPPPCPCRDPERERREEJAZANAAOOBGGDPCDDVJJJXKHHWWJIVWWWXUIHJIIINNNNWWVWWCDDQDCTSEXXXXXXKKFSTTTTTGTGYGGFSFFSXXKXBASSCCCPCCDRRERRFSGGGTQCQQWFTUTHTTSFUHIJIVONHIZZANNOBJJATBUIJBWXLSPCPRSLMGNSADELYFMLSMNNCJJJXRYFZZGCXKESMYFFZABIBVELEYFGAOBOIDESTMSAOVOIJQFZTGSMFLSMAIIUICHUPPJKLTHVPQJWSKSZMNBUGMZOCWQJDEDIPIPJKELSMFCCKDETGTTUOWQDCLLMGAOORLRYZTNUTHNTANVWYFTAOBXKXEFYYYFTALLYKXXLMRREUHTIUZMZMZMAAZZMZNAAASAMZMYNAASFSVUHUUUHUHZZYLXXKDQPQDPBAOOBOGGSTTANFTGTIVHVIMYLZEREERERXWPCOOBOOOOXKWYKYYYQDCEWVVVIVJWJWIDQVVVWWVJJUUUTHUUIVIVREMMZLLYYYYEREEEESEWXXLLDPQLZYMZM
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java