jdk/test/java/lang/management/ThreadMXBean/Semaphore.java
changeset 30365 551470085a1d
parent 30073 989253a902c3
parent 30364 429945d7189c
child 30366 74651100cf08
child 30664 337397328b72
equal deleted inserted replaced
30073:989253a902c3 30365:551470085a1d
     1 /*
       
     2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @bug     4530538
       
    26  * @summary A semaphore utility class.
       
    27  * @author  Mandy Chung
       
    28  */
       
    29 
       
    30 public class Semaphore {
       
    31     private Object go = new Object();
       
    32     private int semaCount;
       
    33     private int waiters = 0;
       
    34 
       
    35     public Semaphore() {
       
    36         semaCount = 0;
       
    37     }
       
    38 
       
    39     public Semaphore(int initialCount) {
       
    40         semaCount = initialCount;
       
    41     }
       
    42 
       
    43     public void semaP() {
       
    44         synchronized (go) {
       
    45             waiters++;
       
    46             while (semaCount == 0) {
       
    47                 try {
       
    48                     go.wait();
       
    49                 } catch (InterruptedException e) {
       
    50                     e.printStackTrace();
       
    51                     throw new InternalError();
       
    52                 }
       
    53             }
       
    54             semaCount--;
       
    55             waiters--;
       
    56         }
       
    57     }
       
    58     public void semaV() {
       
    59         synchronized (go) {
       
    60             semaCount++;
       
    61             go.notify();
       
    62         }
       
    63     }
       
    64 
       
    65     public int getWaiterCount() {
       
    66         synchronized (go) {
       
    67             return waiters;
       
    68         }
       
    69     }
       
    70 
       
    71     public Object getLock() {
       
    72         return go;
       
    73     }
       
    74 }