public class Test{
public static void main(String args[]){
Queue q = new Queue();
Producer p =new Producer(q);
Consumer c = new Consumer(q);
p.start();
c.start();
}
}
//生产者线程
class Producer extends Thread{
Queue q;
public Producer(Queue q){
this.q = q;
}
public void run(){
for(int i=0;i<10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
}
//消费者线程
class Consumer extends Thread{
Queue q;
public Consumer(Queue q){
this.q = q;
}
public void run(){
while(true){
System.out.println("Consumer get:"+q.getValue());
}
}
}
//队列类
class Queue{
int value;
boolean bFull = false;
public synchronized void putValue(int i){ if(!bFull){//首先判断队列中又没有东西,没有则生产
this.value = i;
bFull = true;
}
notify();//提醒消费者去消费
try{
wait();//等待消费者消费完了之后通知它来生产
}
catch(Exception e){
e.printStackTrace();
}
}
public synchronized int getValue(){
if(!bFull){//先判断队列中是否有东西,没有则等待
try{
wait();
}
catch(Exception e){
e.printStackTrace();
}
}
bFull = false;
notify();//此处不能将生产者唤醒。???????、、、、、、、
return value;
}
}程序运行之后发现,消费者消费完第一个后,不能将生产者唤醒。处于死锁状态,不知道怎么解决
public static void main(String args[]){
Queue q = new Queue();
Producer p =new Producer(q);
Consumer c = new Consumer(q);
p.start();
c.start();
}
}
//生产者线程
class Producer extends Thread{
Queue q;
public Producer(Queue q){
this.q = q;
}
public void run(){
for(int i=0;i<10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
}
//消费者线程
class Consumer extends Thread{
Queue q;
public Consumer(Queue q){
this.q = q;
}
public void run(){
while(true){
System.out.println("Consumer get:"+q.getValue());
}
}
}
//队列类
class Queue{
int value;
boolean bFull = false;
public synchronized void putValue(int i){ if(!bFull){//首先判断队列中又没有东西,没有则生产
this.value = i;
bFull = true;
}
notify();//提醒消费者去消费
try{
wait();//等待消费者消费完了之后通知它来生产
}
catch(Exception e){
e.printStackTrace();
}
}
public synchronized int getValue(){
if(!bFull){//先判断队列中是否有东西,没有则等待
try{
wait();
}
catch(Exception e){
e.printStackTrace();
}
}
bFull = false;
notify();//此处不能将生产者唤醒。???????、、、、、、、
return value;
}
}程序运行之后发现,消费者消费完第一个后,不能将生产者唤醒。处于死锁状态,不知道怎么解决
for(int i=0;i <10;i++){
q.putValue(i);
System.out.println("Producer put:"+q.getValue());
}
}
这句错了。改成System.out.println("Producer put:"+i);
打印出第一个产品就是0,线程完了就唤醒生产者线程,此时bFull为true了,就是空了。
生产者接下来还要执行下面的语句,含有getValue():
System.out.println("Producer put:"+q.getValue()); 因为是空,所以生产者线程也等待了
这样就生产者等待,消费者也等待了,死锁了。
public class Productor_buy {
int size = 4; int remain = size; public static void main(String[] args) {
Productor_buy pb = new Productor_buy();
pb.start(); } class Productor implements Runnable { public void run() {
for (int i = 0; true; i++) {
product(i);
}
} } class buyer implements Runnable {
public void run() {
for (int i = 0; true; i++) {
buy(i);
}
}
} public synchronized void product(int i) {
while (remain < 4) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
remain++;
System.out.println("被生产,库存为--->" + remain);
}
notify();
} public synchronized void buy(int i) {
while (remain <= 4) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
remain--;
System.out.println("被消费,库存为--->" + remain);
}
notify();
} public void start() {
Productor pt = new Productor();
buyer by = new buyer();
by.run();
pt.run();
}
}
我上课的时候写的,希望对您有帮助!
5楼是正确的,q.putValue(i);后唤醒了Consumer,然后Producer的System.out.println("Producer put:"+q.getValue());无法被唤醒