package cn.thr; //线程实现的方法一:继承自Thread //线程的例子:总共有10张票,开两个窗口买票。 //票的总数ticket必须是static!!!!否则会有20张票 class TicketThread extends Thread { static int ticket = 10;// 总共10张票 String name; public TicketThread(String name) { this.name = name; } public void run() { while (ticket >= 1) { ticket--; System.out.println(Thread.currentThread().getName() + "卖出的票,票号为:"+ ticket); } } } public class Ticket1 { public static void main(String[] args) { TicketThread ticketThread1 = new TicketThread("线程1"); TicketThread ticketThread2 = new TicketThread("线程2"); ticketThread1.start(); ticketThread2.start(); } }