Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/Barrier.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @bug 453053825* @summary A Barrier utility class.26* @author Mandy Chung27*/2829public class Barrier {30private Object go = new Object();31private int count;32private int waiters = 0;3334public Barrier(int count) {35if (count <= 0) {36throw new IllegalArgumentException();37}38this.count = count;39}4041public void set(int count) {42if (waiters != 0) {43throw new IllegalArgumentException();44}45this.count = count;46this.waiters = 0;47}4849public void await() {50synchronized (go) {51waiters++;52while (count > 0) {53try {54go.wait();55} catch (InterruptedException e) {56e.printStackTrace();57throw new InternalError();58}59}60waiters--;61}62}63public void signal() {64synchronized (go) {65count--;66go.notifyAll();67}68}6970public int getWaiterCount() {71synchronized (go) {72return waiters;73}74}75}767778