当前位置 : 主页 > 编程语言 > java >

java99-车站卖票问题

来源:互联网 收集:自由互联 发布时间:2022-07-04
//买票问题 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();
}
}


运行结果
java99-车站卖票问题_字符串

 


上一篇:java98-线程join使用中断进行另一个
下一篇:没有了
网友评论