日期:2014-05-20  浏览次数:20973 次

Java的多线程问题
直接上源码:
Java code

package com.work;

class Resource{
     int Goods=0;
     boolean choose=false;
}
class Consumer implements Runnable{  //消费者
    Resource r;
    public Consumer(Resource r){
        this.r=r;
    }
    public void run() {
       synchronized (r) {
        while (true) {
            if (r.choose==true) {
                r.Goods--;
                r.choose=false;
                System.out.println("消费了"+r.Goods);
            }
         }
      }        
    }
  }
class Producer implements Runnable{  //生产者
    Resource r;
    public Producer(Resource r){
        this.r=r;
    }
    public void run() {
         synchronized (r) {
            while (true) {
                if (r.choose==false) {
                    r.Goods++;  //1
                    r.choose=true;
                    System.out.println("生产了"+r.Goods);
                }
            }    
          }        
        }
       }

public class Sample {
    
    public static void main(String[] args) {
       Resource r=new Resource();
       Consumer con=new Consumer(r);
       Producer pro=new Producer(r);
       Thread b=new Thread(pro);
       b.start();
       Thread a=new Thread(con);
       a.start();
     }
}




现象:
只答应了 生产了1,然后线程停在哪?

求解释,谢谢各位!!!

------解决方案--------------------
Java code
         synchronized (r) {
            while (true) {
                if (r.choose==false) {
                    r.Goods++;  //1
                    r.choose=true;
                    System.out.println("生产了"+r.Goods);
                }
            }    
          }

------解决方案--------------------
Java code

class Resource {
    int Goods = 0;
    boolean choose = false;
}

class Consumer implements Runnable { // 消费者
    Resource r;

    public Consumer(Resource r) {
        this.r = r;
    }

    public void run() {

        while (true) {
            synchronized (r) {
                if (r.choose == true) {
                    r.choose = false;
                    System.out.println("消费了" + r.Goods--);
                }
            }
        }
    }
}

class Producer implements Runnable { // 生产者
    Resource r;

    public Producer(Resource r) {
        this.r = r;
    }

    public void run() {

        while (true) {
            synchronized (r) {
                if (r.choose == false) {
                    r.Goods++; // 1
                    r.choose = true;
                    System.out.println("生产了" + r.Goods);
                }
            }
        }
    }
}

public class Sample {

    public static void main(String[] args) {
        Resource r = new Resource();
        Consumer con = new Consumer(r);
        Producer pro = new Producer(r);
        Thread b = new Thread(pro);
        b.start();
        Thread a = new Thread(con);
        a.start();
    }
}

------解决方案--------------------
我觉得不会. 执行完后还要回来执行的. 只有同步块的程序执行完,才释放锁.
------解决方案--------------------
class Resource{
int Goods=0;
boolean choose=false;
}
class Consumer implements Runnable{ //消费者
Resource r;
public Consumer(Resource r){
this.r=r;
}
public void run() {
while (true) {
synchronized (r) {
if (r.choose==true) {
r.Goods--;
r.choose=false;
System.out.println("消费了"+r.Goods);
}
}
}
}
 }
class Producer implements Runnable{ //生产者
Resource r;
public Producer(Resource r){
this.r=r;