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

求大神救我出苦海?????
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();
}
}

}
为什么运行dos里面乱码呢?????????????

------解决方案--------------------
估计是因为你编辑Java原文件的编码格式问题。

检查下是 GBK 还是 UTF-8
------解决方案--------------------
搞成英文的试试,还是乱码嘛
------解决方案--------------------
什么dos?是否windows的cmd命令行?

另外,java编译时是否选择了编码:
javac -encoding utf-8