package cn.mutipart.thd; public class TicketTest { public static void main(String[] args) { String lock = "这是线程锁"; Ticket t1 =new Ticket(lock); new Thread(t1).start(); new Thread(t1).start(); } } class Ticket implements Runnable{ private int num =100; public String lock ; public Ticket(String lock){ this.lock = lock ; } public void sendTicket(){ while(true){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (lock) { if(num>0){ num--; System.out.println("还剩下"+num+Thread.currentThread().getName()); }else{ break ; } } } } @Override public void run() { sendTicket(); } }