次程序在eclipse中可运行,但在DOS下运行不了,还有java标识符不能用中文,下面程序却能运行,奇怪!
package jichu.shiyi12;
import java.awt.*;
public class Example11_12
{ public static void main(String args[])
{ new MyFrame();
}
}
class MyFrame extends JFrame implements Runnable,ActionListener
{ /**
*
*/
private static final long serialVersionUID = 1L;
售票员 王小姐;
Thread 张平,李明;
static JTextArea text;
JButton startBuy=new JButton("开始买票");
MyFrame()
{ 王小姐=new 售票员();
张平=new Thread(this);
李明=new Thread(this);
text=new JTextArea(10,30);
startBuy.addActionListener(this);
add(text,BorderLayout.CENTER);
add(startBuy,BorderLayout.NORTH);
setVisible(true);
setSize(360,300);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ try{ 张平.start();
李明.start();
}
catch(Exception exp) {}
}
public void run()
{ if(Thread.currentThread()==张平)
{ 王小姐.售票规则(20);
}
else if(Thread.currentThread()==李明)
{ 王小姐.售票规则(5);
}
}
}
class 售票员
{ int 五元钱的个数=2,十元钱的个数=0,二十元钱的个数=0;
String s=null;
public synchronized void 售票规则(int money)
{ if(money==5) //如果使用该方法的线程传递的参数是5,就不用等待
{ 五元钱的个数=五元钱的个数+1;
s= "给您入场卷您的钱正好";
MyFrame.text.append("\n"+s);
}
else if(money==20)
{ while(五元钱的个数<3)
{ try { wait(); //如果使用该方法的线程传递的参数是20须等待
}
catch(InterruptedException e){}
}
五元钱的个数=五元钱的个数-3;
二十元钱的个数=二十元钱的个数+1;
s="给您入场卷"+" 您给我20,找您15元";
MyFrame.text.append("\n"+s);
}
notifyAll();
}
}