001: //serverSemaphore.java
002: //------------------------
003: //part of YaCy
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2005
007: //
008: //this file is contributed by Martin Thelian
009: //last major change: $LastChangedDate: 2007-10-29 01:43:20 +0000 (Mo, 29 Okt 2007) $ by $LastChangedBy: orbiter $
010: //Revision: $LastChangedRevision: 4181 $
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server;
046:
047: public final class serverSemaphore {
048: private long currentValue = 0;
049: private long maximumValue = Long.MAX_VALUE;
050:
051: public serverSemaphore(long initialValue) {
052: this (initialValue, Long.MAX_VALUE);
053: }
054:
055: protected serverSemaphore(long initialValue, long maxValue) {
056: /* some errorhandling */
057: if (maxValue < initialValue) {
058: throw new IllegalArgumentException(
059: "The semaphore maximum value must not be "
060: + "greater than the semaphore init value.");
061: }
062:
063: if (maxValue < 1) {
064: throw new IllegalArgumentException(
065: "The semaphore maximum value must be greater or equal 1.");
066: }
067:
068: if (initialValue < 0) {
069: throw new IllegalArgumentException(
070: "The semaphore initial value must be greater or equal 0.");
071: }
072:
073: // setting the initial Sempahore Values
074: this .currentValue = initialValue;
075: this .maximumValue = maxValue;
076: }
077:
078: public synchronized void P() throws InterruptedException {
079: this .currentValue--;
080:
081: if (this .currentValue < 0) {
082: try {
083: wait();
084: } catch (InterruptedException e) {
085: this .currentValue++;
086: throw e;
087: }
088: }
089: }
090:
091: public synchronized void V() {
092: if (this .currentValue + 1 == this .maximumValue) {
093: throw new IndexOutOfBoundsException(
094: "The maximum value of the semaphore was reached");
095: }
096:
097: this .currentValue++;
098:
099: if (this .currentValue <= 0) {
100: notify();
101: }
102: }
103: }
|