- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
public class LoadManager
{
public static Integer DEFAULT_MAX_LOAD_COUNTER = 2;
private ConcurrentHashMap<Class, Integer> loadCounters = new ConcurrentHashMap<Class, Integer>();
public void incLoadCounter(Class clazz)
{
synchronized (loadCounters)
{
if (!loadCounters.containsKey(clazz))
{
loadCounters.put(clazz, 0);
}
if (loadCounters.get(clazz) <= DEFAULT_MAX_LOAD_COUNTER)
{
loadCounters.put(clazz, loadCounters.get(clazz) + 1);
}
}
}
public void testLoadCounterAndWait(Class clazz)
{
synchronized (loadCounters)
{
if (!loadCounters.containsKey(clazz))
{
loadCounters.put(clazz, 0);
}
}
try
{
while (loadCounters.get(clazz) > DEFAULT_MAX_LOAD_COUNTER)
{
Thread.sleep(3000L + (long)Math.floor(5000 * Math.random()));
}
}
catch (InterruptedException e) {}
}
public void decLoadCounter(Class clazz)
{
synchronized (loadCounters)
{
if (!loadCounters.containsKey(clazz))
{
loadCounters.put(clazz, 0);
}
if (loadCounters.get(clazz) > 0)
{
loadCounters.put(clazz, loadCounters.get(clazz) - 1);
}
}
}
}
Наверное, нужно было вот это: http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/Semaphore.html
Ну и ещё порадовал способ постановки потоков в "очередь".