I have a question regarding a counting semaphore problem and concurrent programming:

My program needs to run a couple of experiments and for each experiment a 'license' is checked out. Moreover, every experiment runs as a separate Thread so that more than one experiment can be executed at a time.

Since the number of licenses is smaller than the number of experiments to be run, I used a counting semaphore that prohibits a new experiment (thread) to be started if a license is unavailable. (Semaphore is my own implemented semaphore and not the one from java.util.concurrent)

part of my code looks like this:
int no_licenses = 10;
Semaphore s = new Semaphore(no_licenses);
for (int i = 0; i < no_experiments; i++) {

Now my problem is this:
how do I release the license of the experiments that have been finished in an elegant way so that the maximum number of licenses is always employed? My goal is to obtain the finished experiments as fast as possible. Note that experiments can have varying 'runtimes' to be finished, meaning that the first experiment that was started is not necessarily the first that will be finished.

My first (read: bad) idea was to add this piece of code in the loop:
// do nothing when thread is still running
else {

unfortunately, this if-condition prohibits a new experiment to be started if the last experiment is still running, and thus the point of starting multiple threads at once is lost.

suggestions or remarks are welcome!

thanks in advance,