关于java线程的一点问题,急急急急
我感觉自己的是对的,生产者和消费者的问题,我为了简单就是生产一件商品以后缓冲区就满了,不能再生产,当缓冲区空后就不能再消费,但总是会有连续生产两个的结果或消费两个的结果
class Producer extends Thread{
private PCQueue pcqueue;
private int number;
public Producer(PCQueue pcqueue,int number){
this.pcqueue=pcqueue;
this.number=number;
}
public void run(){
for(int i=0;i<10;i++){
pcqueue.put(i);
System.out.println("Producer # "+this.number+" put: "+i);
try{
sleep((int)Math.random()*100);
}catch(Exception e){}
}
}
}
class PCQueue{
private int seq;
private boolean available=false; //缓冲区
public synchronized int get(){
while(available==false){ //缓冲区空的
try{
System.out.println("缓冲区是空的,没有商品可以拿出来"+available);
//System.out.println(available);
wait();
}catch(InterruptedException e){}
}
available=false;
notifyAll();
return seq;
}
public synchronized void put(int value){
while(available==true){ //缓冲区满的
try{
System.out.println("缓冲区满的,不可以再生产商品"+available);
wait();
}catch(InterruptedException e){}
}
available=true;
seq=value;
notifyAll();
}
}
class Consumer extends Thread{
private PCQueue pcqueue;
private int number;
public Consumer(PCQueue pcqueue,int number){
this.pcqueue=pcqueue;
this.number=number;
}
public void run(){
int value=0;
for(int i=0;i<10;i++){
value=pcqueue.get();
System.out.println("Consumer #"+this.number+" got"+value);
}
}
}
public class ProducerConsumer {
public static void main(String args[]){
PCQueue pcqueue = new PCQueue();
Producer p1=new Producer(pcqueue,1);
Consumer c1=new Consumer(pcqueue,1);
p1.start();
c1.start();
}
}
class Producer extends Thread {
private PCQueue pcqueue;
private int number;
public Producer(PCQueue pcqueue, int number) {
this.pcqueue = pcqueue;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
pcqueue.put(i);
try {
sleep((int) Math.random() * 100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class PCQueue {
private int seq;
private boolean available = false; // 缓冲区
public synchronized int get() {
while (available == false) { // 缓冲区空的
try {
System.out.println("缓冲区是空的,没有商品可以拿出来" + available);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Consumer got: " + this.seq);
available = false;
notifyAll();
return seq;
}
public synchronized void put(int value) {
while (available == true) { // 缓冲区满的
try {
System.out.println("缓冲区满的,不可以再生产商品" + available);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Producer # put: " + value);
available = true;
seq = value;
notifyAll();
}
}
class Consumer extends Thread {
private PCQueue pcqueue;
private int number;
public Consumer(PCQueue pcqueue, int number) {
this.pcqueue = pcqueue;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = pcqueue.get();
}
}
}
public class ProducerConsumer {
public static void main(String args[]) {
PCQueue pcqueue = new PCQueue();
Producer p1 = new Producer(pcqueue, 1);
Consumer c1 = new Consumer(pcqueue, 1);
p1.start();
c1.start();
}
}
package com.meritit.dm.help;
class Producer extends Thread {
private PCQueue pcqueue;
public Producer(PCQueue pcqueue) {
this.pcqueue = pcqueue;
}
public void run() {
while (true) {
pcqueue.add();
}
}
}
class PCQueue {
private int seq = 0;
public synchronized void remove() {
while (0 == seq) { // 缓冲区空的
try {
System.out.println("商品已消费,不可再消费,剩余-->" + seq);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
seq = 0;
System.out.println("Consumer remove: 剩余-->" + seq);
notifyAll();
}
public synchronized void add() {
while (1 == seq) { // 缓冲区满的
try {
System.out.println("商品已生产,不可再生产,剩余-->" + seq);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
seq = 1;
System.out.println("Producer # add: 剩余-->" + seq);
notifyAll();
}
}
class Consumer extends Thread {
private PCQueue pcqueue;
public Consumer(PCQueue pcqueue) {
this.pcqueue = pcqueue;
}
public void run() {
while (true) {
pcqueue.remove();
}
}
}
public class ProducerConsumer {
public static void main(String args[]) throws InterruptedException {
PCQueue pcqueue = new PCQueue();
Producer p1 = new Producer(pcqueue);
Consumer c1 = new Consumer(pcqueue);
p1.start();
c1.start();
}
}
商品已消费,不可再消费,剩余-->0
Producer # add: 剩余-->1
商品已生产,不可再生产,剩余-->1
Consumer remove: 剩余-->0
商品已消费,不可再消费,剩余-->0
Producer # add: 剩余-->1
商品已生产,不可再生产,剩余-->1
Consumer remove: 剩余-->0
商品已消费,不可再消费,剩余-->0
Producer # add: 剩余-->1
商品已生产,不可再生产,剩余-->1
Consumer remove: 剩余-->0
商品已消费,不可再消费,剩余-->0
Producer # add: 剩余-->1
商品已生产,不可再生产,剩余-->1