我感觉自己的是对的,生产者和消费者的问题,我为了简单就是生产一件商品以后缓冲区就满了,不能再生产,当缓冲区空后就不能再消费,但总是会有连续生产两个的结果或消费两个的结果
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);
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();
}
}
解决方案 »
- 项目打成jar包后能读取资源文件,但不能修改资源文件的问题
- java打印功能
- 在JAVA中想点击某个图片,让一个程序运行,如何实现?
- 怎样在同一个浏览器网页中从一个applet转换到另一个applet,并且传递数据
- 贡献几个题目。
- 关于java读写配置文件的问题,100分请教
- 请问……
- 随机输入数字时,Exception问题
- 我想知道ie5.0以上,如果不下载java的那个plug-in,对applet支持有多少,有没有api啊?
- 我准备学编程,应怎样开始比较好(我喜欢快),请大家指点,谢谢!!!
- 求高手点拨一个“输出与预期不符”的Java开发代码问题..
- TCP同时发送和接收大批量数据效率问题
System.out.println("Consumer #"+this.number+" got "+value);和
System.out.println("Producer # "+this.number+" put: "+i);不是
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();
}
}打印:
Producer # put: 0
Consumer got: 0
缓冲区是空的,没有商品可以拿出来false
Producer # put: 1
Consumer got: 1
缓冲区是空的,没有商品可以拿出来false
Producer # put: 2
Consumer got: 2
缓冲区是空的,没有商品可以拿出来false
Producer # put: 3
Consumer got: 3
缓冲区是空的,没有商品可以拿出来false
Producer # put: 4
Consumer got: 4
缓冲区是空的,没有商品可以拿出来false
Producer # put: 5
Consumer got: 5
缓冲区是空的,没有商品可以拿出来false
Producer # put: 6
Consumer got: 6
缓冲区是空的,没有商品可以拿出来false
Producer # put: 7
Consumer got: 7
缓冲区是空的,没有商品可以拿出来false
Producer # put: 8
Consumer got: 8
缓冲区是空的,没有商品可以拿出来false
Producer # put: 9
Consumer got: 9
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