java并发包:信号量
来源:互联网 发布:mac speak louder试色 编辑:程序博客网 时间:2024/05/17 23:03
本文转载至:http://blog.csdn.net/a910626/article/details/51900947
信号量为多线程协作提供了更为强大的控制方法。广义上说,信号量是对锁的扩展。无论是内部锁synchronized还是重入锁ReentrantLock,一次都只允许一个线程访问一个资源,而信号量却可以指定多个线程,同时访问某一个资源。信号量主要提供了一下构造函数:
Semaphore(int permits) Creates a Semaphore with the given number of permits and nonfair fairness setting.Semaphore(int permits, boolean fair) Creates a Semaphore with the given number of permits and the given fairness setting.第二个参数指定是否公平
void acquire() Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted. void acquire(int permits) Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted. void acquireUninterruptibly() Acquires a permit from this semaphore, blocking until one is available. void acquireUninterruptibly(int permits) Acquires the given number of permits from this semaphore, blocking until all are available. int availablePermits() Returns the current number of permits available in this semaphore. int drainPermits() Acquires and returns all permits that are immediately available.protected Collection<Thread> getQueuedThreads() Returns a collection containing threads that may be waiting to acquire. int getQueueLength() Returns an estimate of the number of threads waiting to acquire. boolean hasQueuedThreads() Queries whether any threads are waiting to acquire. boolean isFair() Returns true if this semaphore has fairness set true.protected void reducePermits(int reduction) Shrinks the number of available permits by the indicated reduction. void release() Releases a permit, returning it to the semaphore. void release(int permits) Releases the given number of permits, returning them to the semaphore. String toString() Returns a string identifying this semaphore, as well as its state. boolean tryAcquire() Acquires a permit from this semaphore, only if one is available at the time of invocation. boolean tryAcquire(int permits) Acquires the given number of permits from this semaphore, only if all are available at the time of invocation. boolean tryAcquire(int permits, long timeout, TimeUnit unit) Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been interrupted. boolean tryAcquire(long timeout, TimeUnit unit) Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.
这里只讲几个常用的方法:
acquire() 方法尝试获得一个准入的许可。若无法获得,则线程会等待,直到有线程释放一个许可或者当前线程被中断。
acquireUninterruptibly方法和acquire方法类似,但是不响应中断。
tryAcquire尝试获得一个许可,如果成功返回true,失败返回false,它不会进行等待,立即返回。
release用于在线程访问资源结束后,释放一个许可,以使其他等待许可的线程可以进行资源访问
一个案例:
/** * Created by niehongtao on 16/7/12. * 3.1.3允许多个线程同时访问:信号量 */public class SemapDemo implements Runnable { final Semaphore semp = new Semaphore(5); @Override public void run() { try { semp.acquire(); // 模拟耗时操作 Thread.sleep(5000); System.out.println(Thread.currentThread().getId() + ":done"); semp.release(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { ExecutorService exec = Executors.newFixedThreadPool(20); SemapDemo demo = new SemapDemo(); for (int i = 0; i < 20; i++) { exec.execute(demo); } }}
上述代码中,15、16行为临界区,程序会限制执行这段代码的线程数。这里在第7行,声明了一个包含5个许可的信号量。这就意味着同时可以有5个线程进入代码段15,16行。申请信号量使用semp.acquire,在离开时,务必使用semp.release释放信号量。这就和释放锁一个道理。
0 0
- java并发包:信号量
- java并发包学习系列:信号量
- Java并发包之Semaphore信号量
- JAVA并发信号量 Semaphore
- Java并发信号量demo
- Java并发之信号量
- java并发信号量Semaphore
- java高并发程序设计总结四:JDK并发包之信号量Semaphore
- java并发之Semaphore(信号量)
- Java并发系列-22、信号量
- java 并发工具包 -信号量 Semaphore
- java并发之Semaphore(计数信号量)
- 信号量(java并发编程实战5.5.4)
- Java多线程/并发22、信号量Semaphore
- java并发编程学习8--同步器--信号量
- Java并发包--AbstractQueuedSynchronizer
- java 并发包
- java并发包
- c++实验4—输出星号图
- 微信支付统一下单,签名错误
- CSS3实现关闭按钮
- 二元查找树
- hdu2674 N!Again
- java并发包:信号量
- thinkphp5 事务
- 触摸屏校准之tslib
- SYN Flood
- js洗牌算法实现产生随机数组
- Ubuntu Android GCC 版本配置
- ofo缘分星期三 我的骑行编号是a255979
- OpenCV Python教程(1、图像的载入、显示和保存)
- java中sleep()和wait()的区别