(3)Semaphore 动态添加许可
2018-03-29 本文已影响12人
码出高效
有参方法acquire(int permits)的功能是每次调用1次方法,就使用x个许可




代码中一共有10个许可,每次执行acquire(2),代码耗费2个,所以10/2=5,说明同一时间有5个线程运行执行。
如果多次调用Semaphore类的release()或release(int)方法时,还可以动态增加permits的个数.


有参方法acquire(int permits)的功能是每次调用1次方法,就使用x个许可
代码中一共有10个许可,每次执行acquire(2),代码耗费2个,所以10/2=5,说明同一时间有5个线程运行执行。
如果多次调用Semaphore类的release()或release(int)方法时,还可以动态增加permits的个数.