Java并发编程:核心方法与框架
上QQ阅读APP看书,第一时间看更新

1.1.3 方法acquire(int permits)参数作用及动态添加permits许可数量

有参方法acquire(int permits)的功能是每调用1次此方法,就使用x个许可。

创建Java项目,名称为Semaphore_acquire(int permits)_release(int permits),创建类Service.java代码如下:


        package service;
        import java.util.concurrent.Semaphore;
        public class Service {
            private Semaphore semaphore = new Semaphore(10);
            public void testMethod() {
                try {
                    semaphore.acquire(2);
                  System.out.println(Thread.currentThread().getName()
                          + " begin timer=" + System.currentTimeMillis());
                  int sleepValue = ((int) (Math.random() * 10000));
                  System.out.println(Thread.currentThread().getName() + " 停止了"
                          + (sleepValue / 1000) + "秒");
                  Thread.sleep(sleepValue);
                  System.out.println(Thread.currentThread().getName()
                          + "    end timer=" + System.currentTimeMillis());
                  semaphore.release(2);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }

线程类ThreadA.java代码如下:


      package extthread;
      import service.Service;
      public class ThreadA extends Thread {
          private Service service;
          public ThreadA(Service service) {
              super();
              this.service = service;
          }
          @Override
          public void run() {
              service.testMethod();
          }
      }

类Run.java代码如下:


      package test;
      import service.Service;
      import extthread.ThreadA;
      public class Run {
          public static void main(String[] args) throws InterruptedException {
              Service service = new Service();
              ThreadA[] a = new ThreadA[10];
              for (int i = 0; i < 10; i++) {
                  a[i] = new ThreadA(service);
                        a[i].start();
                    }
                }
            }

程序运行效果如图1-7所示。

图1-7 运行结果

在代码中一共有10个许可,每次执行semaphore. acquire(2);代码时耗费掉2个,所以10/2=5,说明同一时间只有5个线程允许执行acquire()和release()之间的代码。

如果多次调用Semaphore类的release()或release(int)方法时,还可以动态增加permits的个数。

创建测试用的项目addPermitsCount,类Run.java代码如下:


              package test;
              import java.util.concurrent.Semaphore;
              public class Run {
                  public static void main(String[] args) {
                      try {
                          Semaphore semaphore = new Semaphore(5);
                          semaphore.acquire();
                          semaphore.acquire();
                          semaphore.acquire();
                          semaphore.acquire();
                          semaphore.acquire();
                          System.out.println(semaphore.availablePermits());
                          semaphore.release();
                          semaphore.release();
                          semaphore.release();
                          semaphore.release();
                          semaphore.release();
                          semaphore.release();
                          System.out.println(semaphore.availablePermits());
                          semaphore.release(4);
                          System.out.println(semaphore.availablePermits());
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }

程序运行后的效果如图1-8所示。

图1-8 成功动态添加许可

此实验说明构造参数new Semaphore(5);中的5并不是最终的许可数量,仅仅是初始的状态值。