import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) {
int[] runningThreads = { 0 };
int[] taskcount = { 10 };
Lock myLock = new ReentrantLock(true);
int maxThreadQty = 3;
while ((taskcount[0] > 0) && (runningThreads[0] < maxThreadQty)) {
myLock.lock();
runningThreads[0]++;
myLock.unlock();
new Thread("T") {
public void run() {
myLock.lock();
taskcount[0]--;
runningThreads[0]--;
myLock.unlock();
}
}.start();
}
}
}