01: /*
02: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/db/TKLimiter.java,v 1.13 2002/01/14 12:52:07 markus Exp $
03: *
04: */
05: package com.teamkonzept.db;
06:
07: import com.teamkonzept.lib.*;
08: import org.apache.log4j.Category;
09:
10: /**
11: * @author
12: * @version
13: */
14: public class TKLimiter {
15:
16: private static final Category CAT = Category
17: .getInstance(TKLimiter.class);
18: public int limit;
19: public int used;
20: public int pending;
21:
22: public TKLimiter() {
23: limit = 0;
24: used = 0;
25: pending = 0;
26: }
27:
28: public TKLimiter(int aLimit) {
29: limit = aLimit;
30: used = 0;
31: pending = 0;
32: }
33:
34: public synchronized void newLimit(int aLimit) {
35: limit = aLimit;
36: if ((limit <= 0) && (pending > 0)) {
37: notifyAll();
38: } else if ((used < limit) && (pending > 0)) {
39: notify();
40: }
41: }
42:
43: public synchronized void take() {
44: pending++;
45: while ((used >= limit) && (limit > 0)) {
46:
47: try {
48: wait();
49: } catch (InterruptedException ie) {
50: CAT.error(ie);
51: }
52: }
53:
54: pending--;
55: used++;
56: }
57:
58: public synchronized void free() {
59: used--;
60: if ((limit <= 0) && (pending > 0)) {
61: notifyAll();
62: } else if ((used < limit) && (pending > 0)) {
63: notify();
64: }
65: }
66: }
|