码迷,mamicode.com
首页 > 其他好文 > 详细

一个简单的并发程序

时间:2015-06-04 17:24:06      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:并发

package p2;
public class TestThread {
    
    public static void main(String args[]) throws InterruptedException{        
        
        Thread1 t1 = new Thread1();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        new Thread(t1).start();
        t1.stop();    
                    
    }
}
class Thread1 extends Thread{
    private int tickets = 20;
     public void run(){
         while(true){
             synchronized(this){
                 if(tickets>0){
                     try {
                            Thread.sleep(10l);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                     System.out.println(Thread.currentThread().getName()+"售票"+tickets--);
                 }
                 else{
                     break;
                 }                 
             }            
         }          
     }
}

模拟售票功能,总票数是20张,同时分四个窗口售票

一个简单的并发程序

标签:并发

原文地址:http://smileyes.blog.51cto.com/6027700/1658494

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!