本帖最后由 刘海陆 于 2013-4-3 14:30 编辑
我在编译一下代码的时候,出现了乱码,是什么情况?
class Res{
private String name;
private int count=0;
private boolean flag = false;
public synchronized void set(String name){
if(flag)
try{wait();}catch(Exception e){};
this.name = name;
System.out.println(Thread.currentThread().getName()+"----"+name+(++count));
flag = true;
notify();
}
public synchronized void out(){
if(!flag)
try{wait();}catch(Exception e){};
System.out.println(Thread.currentThread().getName()+"-----------"+name+count);
flag = false;
notify();
}
}
class Pro implements Runnable{
private Res s;
Pro(Res s){
this.s = s;
}
public void run(){
while(true){
s.set("商品");
}
}
}
class Con implements Runnable{
private Res s;
Con(Res s){
this.s = s;
}
public void run(){
while(true){
s.out();
}
}
}
class ProConDemo
{
public static void main(String[] args)
{
Res s = new Res();
new Thread(new Pro(s)).start();
new Thread(new Pro(s)).start();
new Thread(new Con(s)).start();
new Thread(new Con(s)).start();
}
}
编译时候出现的乱码:
|
|