互斥问题请指教(源代码如下:)
//我的问题是为什么缓冲区满了后生产者会继续执行push()操作
package stone.pandc;
public class PandC{
public static void main(String arg[]){
BufStack buffer = new BufStack();
Runnable source = new Producer(buffer);
Runnable destination = new Consumer(buffer);
Thread p1 = new Thread(source);
// Thread p2 = new Thread(source);
Thread c1 = new Thread(destination);
// Thread c2 = new Thread(destination);
p1.start();
// p2.start();
c1.start();
// c2.start();
}
}
// the buffer
class BufStack {
private int index = 0;
private char buffer[] = new char[6];
private int pCounter = 1;
private int cCounter = 1;
//
public synchronized void push(char c){
while(index == buffer.length){
try{
System.out.println("the buffer is full ! waiting for the consumer !"+(pCounter++));
this.wait();
}
catch(InterruptedException e){ }
}
this.notify();
buffer[index] = c;
index++;
//System.out.println("thread "+"produced: "+c+" ");
}
//
public synchronized char pop( ){
while(index == 0){
try{
System.out.println("the buffer is empty ! waiting for the producer !"+(cCounter++));
this.wait();
}
catch(InterruptedException e){ }
}
this.notify();
index--;
return buffer[index];
}
}
// the producer thread
class Producer implements Runnable{
BufStack mybuf;
public Producer(BufStack pbuf){
mybuf = pbuf;
}
public void run( ){
char c;
for(int i = 0;i < 20;i ++){
c=(char)(Math.random()*26+ 'A');
mybuf.push(c);
System.out.println("thread "+"produced: "+c+" "+i);
try{
Thread.sleep((int)(Math.random( )*1000));
}
catch (InterruptedException e){ }
}
}
}
//the consumer thread
class Consumer implements Runnable{
BufStack mybuf;
public Consumer(BufStack pbuf){
mybuf = pbuf;
}
public void run( ){
char c;
for(int i = 0;i < 20;i ++){
c = mybuf.pop();
System.out.println("Consumed: "+c);
try{
Thread.sleep((int)(Math.random( )*1500));
}
catch (InterruptedException e){ }
}
}
}
package stone.pandc;
public class PandC{
public static void main(String arg[]){
BufStack buffer = new BufStack();
Runnable source = new Producer(buffer);
Runnable destination = new Consumer(buffer);
Thread p1 = new Thread(source);
// Thread p2 = new Thread(source);
Thread c1 = new Thread(destination);
// Thread c2 = new Thread(destination);
p1.start();
// p2.start();
c1.start();
// c2.start();
}
}
// the buffer
class BufStack {
private int index = 0;
private char buffer[] = new char[6];
private int pCounter = 1;
private int cCounter = 1;
//
public synchronized void push(char c){
while(index == buffer.length){
try{
System.out.println("the buffer is full ! waiting for the consumer !"+(pCounter++));
this.wait();
}
catch(InterruptedException e){ }
}
this.notify();
buffer[index] = c;
index++;
//System.out.println("thread "+"produced: "+c+" ");
}
//
public synchronized char pop( ){
while(index == 0){
try{
System.out.println("the buffer is empty ! waiting for the producer !"+(cCounter++));
this.wait();
}
catch(InterruptedException e){ }
}
this.notify();
index--;
return buffer[index];
}
}
// the producer thread
class Producer implements Runnable{
BufStack mybuf;
public Producer(BufStack pbuf){
mybuf = pbuf;
}
public void run( ){
char c;
for(int i = 0;i < 20;i ++){
c=(char)(Math.random()*26+ 'A');
mybuf.push(c);
System.out.println("thread "+"produced: "+c+" "+i);
try{
Thread.sleep((int)(Math.random( )*1000));
}
catch (InterruptedException e){ }
}
}
}
//the consumer thread
class Consumer implements Runnable{
BufStack mybuf;
public Consumer(BufStack pbuf){
mybuf = pbuf;
}
public void run( ){
char c;
for(int i = 0;i < 20;i ++){
c = mybuf.pop();
System.out.println("Consumed: "+c);
try{
Thread.sleep((int)(Math.random( )*1500));
}
catch (InterruptedException e){ }
}
}
}
...全文
相关推荐
对HANOI问题的详细程序描述!如果你有觉得有问题的地方请指教!希望能帮助你!
对百鸡问题的详细程序描述!如果你有觉得有问题的地方请指教!希望能帮助你,你也知道这门课很难学吧!
linux上实现多进程和多线程实现同步互斥(源代码)
在linux上分别用多进程和多线程实现的同步互斥操作(源代码)
皇后问题的程序描述对百鸡问题的详细程序描述!如果你有觉得有问题的地方请指教!希望能帮助你
对皇后问题的详细程序描述!如果你有觉得有问题的地方请指教!希望能帮助你,你也知道这门课很难学吧!
1:500地形图请多多指教
我做了幅1:500地形图请各位高人多多指教
简易魔方生成器 不足请指教 请关注新版本
生成简易的魔方,能生成3——501阶的简易魔方,并保存起来,速度还可以,自己写的!不足请指教! 请关注新版本 请关注新版本 请关注新版本 请关注新版本


Java SE
