首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求大神救小弟我出苦海?

2012-09-12 
求大神救我出苦海?????public class ThreadTest{public static void main (String [] args){ Resource res

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

热点排行