package cn.thr; //线程实现的方法二:实现runnable接口.多采用此方法来避免单继承的局限性 //线程的例子:总共有10张票,开两个窗口买票。 class TicketDemo2 implements Runnable { int ticket = 20; @Override public void run() { while (ticket >= 1) { ticket--; System.out.println(Thread.currentThread().getName() + "卖出的票,票号为:"+ ticket); } } } public class Ticket2 { public static void main(String[] args) { TicketDemo2 demo = new TicketDemo2(); Thread thread1 = new Thread(demo);// 窗口1 Thread thread2 = new Thread(demo);// 窗口2 thread1.start(); thread2.start(); } }