View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junitcore.pc;
20  
21  import java.util.concurrent.Semaphore;
22  
23  /**
24   * @author Tibor Digana (tibor17)
25   * @see Balancer
26   * @since 2.16
27   */
28  final class ThreadResourcesBalancer implements Balancer {
29      private final Semaphore balancer;
30  
31      private final int numPermits;
32  
33      /**
34       * <code>fair</code> set to false.
35       *
36       * @param numPermits number of permits to acquire when maintaining concurrency on tests.
37       *                   Must be &gt;0 and &lt; {@link Integer#MAX_VALUE}.
38       * @see #ThreadResourcesBalancer(int, boolean)
39       */
40      ThreadResourcesBalancer(int numPermits) {
41          this(numPermits, false);
42      }
43  
44      /**
45       * @param numPermits number of permits to acquire when maintaining concurrency on tests.
46       *                   Must be &gt;0 and &lt; {@link Integer#MAX_VALUE}.
47       * @param fair       {@code true} guarantees the waiting schedulers to wake up in order they acquired a permit
48       * @throws IllegalArgumentException if <code>numPermits</code> is not positive number
49       */
50      ThreadResourcesBalancer(int numPermits, boolean fair) {
51          if (numPermits <= 0) {
52              throw new IllegalArgumentException(String.format("numPermits=%d should be positive number", numPermits));
53          }
54          balancer = new Semaphore(numPermits, fair);
55          this.numPermits = numPermits;
56      }
57  
58      /**
59       * Acquires a permit from this balancer, blocking until one is available.
60       *
61       * @return {@code true} if current thread is <b>NOT</b> interrupted
62       *         while waiting for a permit.
63       */
64      @Override
65      public boolean acquirePermit() {
66          try {
67              balancer.acquire();
68              return true;
69          } catch (InterruptedException e) {
70              return false;
71          }
72      }
73  
74      /**
75       * Releases a permit, returning it to the balancer.
76       */
77      @Override
78      public void releasePermit() {
79          balancer.release();
80      }
81  
82      @Override
83      public void releaseAllPermits() {
84          balancer.release(numPermits);
85      }
86  }