本帖最后由 136616244 于 2014-5-8 21:38 编辑
- package 多线程;
- class TicketDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SaleTicket t1 = SaleTicket.getIntance();
- SaleTicket t2 = SaleTicket.getIntance();
- SaleTicket t3 =SaleTicket.getIntance();
- SaleTicket t4 = SaleTicket.getIntance();
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- // t1.start();
- }
-
- }
- class SaleTicket extends Thread{
- private int Ticket = 100;
- private SaleTicket(){}
- static SaleTicket t = new SaleTicket();
- public static SaleTicket getIntance(){
- return t;
- }
-
- public synchronized void run(){
- while(Ticket>0){
- System.out.println(Thread.currentThread().getName()+"..."+Ticket--);
- }
- }
- }
复制代码 |
|