//买票问题 class Xc9 implements Runnable { public static int chepiao = 100 ; static String aa = new String ( "1" ); //字符串随意定义,定义在函数上面 public void run (){ while ( true ){ synchronized ( aa ) //可以修饰代码
class Xc9 implements Runnable{
public static int chepiao=100;
static String aa=new String("1");//字符串随意定义,定义在函数上面
public void run(){
while (true){
synchronized (aa) //可以修饰代码块,又可以修饰函数
/*try{
Thread.sleep(50);
}catch (Exception e){
}*/
{
if(chepiao>0){
System.out.println("第"+Thread.currentThread().getName()+"个车站正在卖第"+(101-chepiao)+"张车票");
--chepiao;
}else {
break;
}
}
}
}
}
public class test34 {
public static void main(String[] args){
Xc9 xc81=new Xc9();
Thread ee=new Thread(xc81);
ee.start();
Xc9 xc82=new Xc9();
Thread ff=new Thread(xc82);
ff.start();
}
}
运行结果