为啥我这个线程问题乱码?
public class ThreadTest
{
public static void main (String [] args)
{
Resource res = new Resource ();
new Thread (new Pro (res)).start();
new Thread (new Con (res)).start ();
}
}
class Resource
{
private String name;
private int count = 0;
boolean flag = false;
public void add (String name)
{
synchronized (this)
{
while (flag)
{
try
{
this.wait();
}
catch (Exception e)
{
return;
}
}
this.name = name+count++;
System.out.println ("产生"+this.name);
flag = true;
this.notifyAll();
}
}
public void get ()
{
synchronized (this)
{
while (!flag)
{
try
{
this.wait();
}
catch (Exception e)
{
return;
}
}
System.out.println ("消费"+this.name);
flag = false;
this.notifyAll();
}
}
}
class Pro implements Runnable
{
private Resource res;
public Pro(Resource res)
{
this.res = res;
}
public void run ()
{
while (true)
{
res.add("馒头");
}
}
}
class Con implements Runnable
{
private Resource res;
public Con(Resource res)
{
this.res = res;
}
public void run ()
{
while (true)
{
res.get();
}
}
}
------解决方案--------------------
完全没看懂你想问啥。。。