java多线程模拟停车系统

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.SynchronousQueue;

import sun.awt.geom.AreaOp.IntOp;

/**
 * 
 * @author Administrator
 *    多线程停车系统模型
 */
public class test2 {
//    private static final SynchronousQueue<String> sq = new SynchronousQueue<String>();
    //新建5个停车位
    Random random = new Random();

    private static final BlockingQueue<String> carPakingSpace = new ArrayBlockingQueue<String>(5);
//    private void parkingSeat(String cars) {
//        System.out.println(cars+"停车");
//    }
    public static void main(String[] args) {
        new test2().Cars();
    }
//    车位系统
//    privat5e void 

    //初始化车辆
    private void Cars() {
        
    
        //现有20辆车子要来停车,但是每1秒只能停1辆车子
        final Semaphore _Semaphore = new Semaphore(1);
        for(int i=0 ; i<20; i++) {
            new Thread(new Runnable() {
                
                public void run() {
//                while(true) {
                    
                    try {
                        _Semaphore.acquire();
                        pakingCars(Thread.currentThread().getName());
            
                        //每一秒只能停1辆车
                        Thread.sleep(1000);
                        _Semaphore.release();
                        
                        
                        
                        //模拟停车的时间
                        Thread.sleep(random.nextInt(4000)+1000);
                        //离开了
                        leaveCars(Thread.currentThread().getName());
                        
                        
                    
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                    
                }
            }).start();
        }
    }
    
    //准备离开的车
    private void leaveCars(String cars) {
        try {
            System.out.println(carPakingSpace.take()+"-----离开了");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //停车
    private void pakingCars(String cars) {
        try {
            carPakingSpace.put(cars);
            System.out.println(cars+": 停车");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
Thread-0: 停车
Thread-1: 停车
Thread-2: 停车
Thread-0-----离开了
Thread-3: 停车
Thread-4: 停车
Thread-5: 停车
Thread-1-----离开了
Thread-6: 停车
Thread-2-----离开了
Thread-3-----离开了
Thread-7: 停车
Thread-4-----离开了
Thread-8: 停车
Thread-5-----离开了
Thread-6-----离开了
Thread-9: 停车
Thread-10: 停车
Thread-7-----离开了
Thread-11: 停车
Thread-8-----离开了
Thread-9-----离开了
Thread-12: 停车
Thread-13: 停车
Thread-10-----离开了
Thread-14: 停车
Thread-15: 停车
Thread-11-----离开了
Thread-16: 停车
Thread-12-----离开了
Thread-17: 停车
Thread-13-----离开了
Thread-18: 停车
Thread-14-----离开了
Thread-19: 停车
Thread-15-----离开了
Thread-16-----离开了
Thread-17-----离开了
Thread-18-----离开了
Thread-19-----离开了

 

posted on 2015-10-17 23:34  freedom's_blog  阅读(1235)  评论(0编辑  收藏  举报

导航