我正在尝试学习线程,因此我编写了一个示例生产者消费者问题,其中生产者产生从 1 到 10 的数字,而消费者必须显示它们。但只有消费者显示数字 1 并停止。
正如我所说,该程序写得不好,可能很荒谬,但我仍然想弄清楚为什么没有打印从 1 到 10 的所有数字的原因,因为当我编写代码而不是从示例中时,我会记得最好。
我正在使用两个变量来跟踪生产者或消费者活动的完成情况,以便我可以执行另一个。
/ getStatus(Consumer C) 传递消费者引用,以便生产者获得消费者的引用 .. 它可以用来了解消费者的状态 .. 就是这样 .. /
import java.lang.Math;
public class Hello{
public static void main(String args[]) {
System.out.println("---1");
new Consumer().start();
}
}
class Producer extends Thread{
public int produce = 0;
public Consumer consumerObj =null;
int count = 1;
boolean producerStatus = false;
public void run(){
System.out.println("---4");
synchronized(this){
do{
System.out.println("---6");
produce = produce+1;
producerStatus = true;
notify();
consumerObj.consumerStatus = false;
System.out.println("---9");
count = count+1;
}while(count<=10 && consumerObj.getStatus());
}
}
public int getItem(){
return produce;
}
public boolean getStatus(Consumer c){
consumerObj = c;
return producerStatus;
}
}
class Consumer extends Thread{
boolean consumerStatus = false;
int count =0;
public void run(){
System.out.println("---2");
Producer p = new Producer();
p.getStatus(this);
p.start();
try{
System.out.println("---3");
synchronized(p){
System.out.println("---5");
p.wait();
System.out.println("---8");
}
}
catch(Exception e){
System.out.println("exception");
}
synchronized(p){
try{
while(p.getStatus(this) && count<=9 ){
System.out.println("---7");
int consume = p.getItem();
System.out.println("the produced item is ----->"+consume);
count = count+1;
p.producerStatus = false;
consumerStatus = true;
p.wait();
System.out.println("---10");
}
}
catch(Exception e){
System.out.println("exception");
}
}
}
public boolean getStatus(){
return consumerStatus;
}
}
输出 :
---1
---2
---3
---5
---4
---6
---9
---8
---7
the produced item is ----->1
---10
在.. Suraj 的输入之后.. 现在程序运行良好.. 见下文..
导入java.lang.Math;公共课你好{
public static void main(String args[]) {
System.out.println("---1");
new Consumer().start();
}
}
class Producer extends Thread{
public int produce = 0;
public Consumer consumerObj =null;
int count = 1;
boolean producerStatus = false;
public void run(){
System.out.println("---4");
do{
if(consumerObj.getStatus()){
System.out.println("---6");
produce = produce+1;
System.out.println("---9 -- >produce is -->"+produce);
producerStatus = true;
synchronized(this){
notify();
System.out.println("---6.111");
}
consumerObj.consumerStatus = false;
count = count+1;
}
}while(count<=10);
}
public int getItem(){
return produce;
}
public boolean getStatus(Consumer c){
consumerObj = c;
return producerStatus;
}
}
class Consumer extends Thread{
boolean consumerStatus = true;
int count =1;
public void run(){
System.out.println("---2");
Producer p = new Producer();
p.getStatus(this);
p.start();//can a thread1 wait on an thread2 before the thread2 hass tarted and in this case wll notify on the scnd thread reaally notify therad1 ..
try{
System.out.println("---3");
synchronized(p){
System.out.println("---5");
p.wait();
System.out.println("---8");
}
}
catch(Exception e){
System.out.println("exception");
}
try{
while(count<=10 ){
System.out.println("---7");
int consume = p.getItem();
System.out.println("the produced item is ----->"+consume);
count = count+1;
p.producerStatus = false;
consumerStatus = true;
synchronized(p){
p.wait();
System.out.println("---10");
}
}
}
catch(Exception e){
System.out.println("exception");
}
}
public boolean getStatus(){
return consumerStatus;
}
}