public class Produ
{
public static void main(String[] args)
{
pro pr=new pro();
con co=new con();
pr.start();
co.start();
}
}
class pro extends Thread
{
Queue q;
int val;
public void run()
{
for(int i=0;i<10;i++)
{
q.put(i);
try {
Thread.sleep(10);
    }
    catch (Exception ex) {
    }
System.out.println ("produce: "+i);
}
}

class con extends Thread
{
Queue q;
public void run()
{
System.out.println (q.get());

}

}
class Queue
{
int value;
public void put(int i)
{
    value=i;
}
public int get()
{
return value;
}
}

解决方案 »

  1.   

    我也刚开始学不是很久. 我个人觉得:Queue class needs an array or something like Vector to keep track with the "int i" you want to put in; Otherwise every time you set value to i, which might not be what you wanted.And second of all, I guess the "Queue q" in the con class is not the same as it is in the pro class. I suggest you make it static, then you can access it everywhere.Third, the con class should be "Con"(Captical naming convention).Fouth, It seems no necessary to have "int val" in the Pro class. 
      

  2.   

    Queue q;改为Queue q=new Queue();
    另外你的类名con与系统里的类名冲突,可以改为con1,即:
    con co=new con();--->con1 co=new con1();class con extends Thread---->class con1 extends Thread