package duoxiancheng;
class Ticket1 implements Runnable
{
private static int ticket=100;
public void run()
{
while(true)//执行100循环
{
show();
}
}
public static synchronized void show()//同步函数,其锁是this若该函数被静态修饰则其锁是类名.class
{
if(ticket>0)
System.out.println(Thread.currentThread().getName()+" "+ticket--);
}
}
public class ThreadTicketDemo1 {
public static void main(String[] args) {
Ticket1 t=new Ticket1();
Thread d1=new Thread(t);
Thread d2=new Thread(t);
d1.start();
d2.start();
}
}