多线程11-Semaphore信号灯

1.概念

       Semaphore可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数

2.案例

package org.lkl.thead.foo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * Function : 信号灯
 * @author : Liaokailin
 * CreateDate : 2014-6-19
 * version : 1.0
 */
public class SemaphoreFoo {
    
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool() ;
        final Semaphore sp = new Semaphore(3) ; //3个信号等
        for(int i= 1 ;i<=10 ;i++){
            Runnable runnable = new Runnable() {
                
                @Override
                public void run() {
                    

                        //获取一个信号灯
                        try {
                            sp.acquire() ;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        
                        System.out.println("线程:"+Thread.currentThread().getName()+
                                " 获得信号灯 开始工作,当前并发的线程有:"+(3-sp.availablePermits()));
                        
                        try {
                            Thread.sleep((long)(Math.random()*10000)) ;//线程休眠 (模拟线程的业务执行)
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } 
                        
                        System.out.println("线程:"+Thread.currentThread().getName()+"即将释放信号灯.");
                            sp.release() ; //释放信号灯 此时其他线程就可以抢占释放的信号灯
                            
                            //下面代码有时候执行不准确,因为其没有和上面的代码合成原子单元
                            System.out.println("线程" + Thread.currentThread().getName() + 
                                    "已离开,当前已有" + (3-sp.availablePermits()) + "个并发");
                        
                }
            };
            
            
            threadPool.execute(runnable) ; //线程启动.
        }
    }
}

 

 

posted @ 2014-06-19 15:55  廖凯林  阅读(459)  评论(0编辑  收藏  举报