KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > db > TKLimiter


1 /*
2  * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/db/TKLimiter.java,v 1.13 2002/01/14 12:52:07 markus Exp $
3  *
4  */

5 package com.teamkonzept.db;
6
7 import com.teamkonzept.lib.*;
8 import org.apache.log4j.Category;
9
10 /**
11  * @author
12         * @version
13  */

14 public class TKLimiter {
15
16     private static final Category CAT = Category.getInstance(TKLimiter.class);
17     public int limit;
18     public int used;
19     public int pending;
20
21     public TKLimiter()
22     {
23         limit = 0;
24         used = 0;
25         pending = 0;
26     }
27
28     public TKLimiter( int aLimit )
29     {
30         limit = aLimit;
31         used = 0;
32         pending = 0;
33     }
34     
35     public synchronized void newLimit( int aLimit )
36     {
37         limit = aLimit;
38         if ((limit <= 0) && (pending > 0))
39             {
40                 notifyAll();
41             }
42         else if ((used < limit) && (pending > 0))
43             {
44                 notify();
45             }
46     }
47     
48     public synchronized void take()
49     {
50         pending++;
51         while ((used >= limit) && (limit > 0)) {
52         
53             try {
54                 wait();
55             }
56             catch( InterruptedException JavaDoc ie ) { CAT.error(ie);}
57         }
58         
59         pending--;
60         used++;
61     }
62     
63     public synchronized void free()
64     {
65         used--;
66         if ((limit <= 0) && (pending > 0))
67             {
68                 notifyAll();
69             }
70         else if ((used < limit) && (pending > 0))
71             {
72                 notify();
73             }
74     }
75 }
76
77
Popular Tags