Java: Wait for unknown number of unsynchronzied tasks
===

I have a queue of tasks, and a thread that peek the queue once in a few seconds and if there is a task it performs it.

I have another code section (in another thread of course), that creates tasks in a loop (I can't know the number of tasks in advance from outside the loop) and insert them to the queue. The tasks contains some 'result' object, and the external thread (which created those tasks) need to wait for all the tasks to finish and finally get the result from each one of them.
The problem is that I can't pass java Semaphore\CountDownLatch etc to the result object since I don't know the number of monitors in advance. 
I also can't use an Executor that uses invokeAll or wait for the Future object since the tasks are unsynchrnized (the external thread just pust the task to a queue and another thread will execute the task when he have time for this).

The only solution I've had in mind is to create some 'Inverted Semaphore' class that holds a set of results and a monitors counter. The getResult function will check if the counter == 0 and if the answer is yes will notify some lock object, and the getResult function will wait for this lock:

    public class InvertedSemaphore<T> {
        Set<T> resultSet;
        int usages;
        final Object c;
    
        public BlockingSemaphore() {
            resultSet = Collections.synchronizedSet(new HashSet<T>());
            usages = 0;
            c = new Object();
        }
    
        public void addResult(T result) {
            resultSet.add(result);
        }
    
        public void addResults(Set<T> result) {
            resultSet.addAll(result);
        }
    
    
        public void acquire() {
            usages++;
        }
    
        public void release() {
            synchronized (c) {
                if (--usages == 0) {
                    c.notify();
                }
            }
        }
    
        public Set<T> getResults() {
            synchronized (c) {
                try {
                    while (usages > 0) {
                        c.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return resultSet;
        }
    
    }

Each addTask method will invoke semaphore.acquire, and each of the (unsynchronized) tasks will invoke semaphore.release in the end of the task.

It sounds pretty complicated and I'm pretty sure there is a better solution for this in java concurrent library or something.

Any idea will be appriciated:)