线程案例-卖票2-继承

来源:互联网 发布:Campbell biology 淘宝 编辑:程序博客网 时间:2024/06/05 04:35
class A extends Thread
{
public static int tickets=100;
static String str= new String("哈哈");
public void run(){
while(true)
{
synchronized(str)
{


if(tickets>0)
{
System.out.printf("哈哈%s哈哈%d哈哈\n",Thread.currentThread().getName(),tickets);
--tickets;
}
else
break;
}
try{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println("异常");
}

}

}


}
public class Maipiao2{
public static void main(String[] args){
A aa=new A();
Thread t1=new Thread(aa);
t1.start();
Thread t2=new Thread(aa);
t2.start();
}
}
原创粉丝点击