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