package com.multith.java; public class Ticket implements Runnable{ private int num=100; @Override public void run() { while(true) { synchronized (this) { //同步代码块 if(num>0) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } num--; System.out.println(Thread.currentThread().getName()+ " 卖出一张票,还剩:"+ num + "张"); }else { System.out.println(Thread.currentThread().getName()+" 票已售完!"); break; } } } } public static void main(String[] args) { Ticket tic = new Ticket(); Thread t1 = new Thread(tic,"窗口A"); Thread t2 = new Thread(tic,"窗口B"); Thread t3 = new Thread(tic,"窗口C"); t1.start(); t2.start(); t3.start(); } }