package net.johnewart.gearman.engine.core;

import net.johnewart.gearman.common.interfaces.EngineWorker;

import java.util.HashSet;
import java.util.concurrent.atomic.AtomicLong;

public class WorkerPool {
    private final HashSet<EngineWorker> sleepingWorkers;
    private final HashSet<EngineWorker> activeWorkers;
    private final AtomicLong numberOfConnectedWorkers;

    public WorkerPool(final String name) {
        sleepingWorkers = new HashSet<>();
        activeWorkers = new HashSet<>();
        numberOfConnectedWorkers = new AtomicLong(0);
    }

    public void addWorker(final EngineWorker worker) {
        activeWorkers.add(worker);
        numberOfConnectedWorkers.incrementAndGet();
    }

    public void removeWorker(final EngineWorker worker) {
        sleepingWorkers.remove(worker);
        activeWorkers.remove(worker);
        numberOfConnectedWorkers.decrementAndGet();
    }

    public void markSleeping(final EngineWorker worker) {
        sleepingWorkers.add(worker);
    }

    public void wakeupWorkers() {
        for (final EngineWorker w : sleepingWorkers) {
            w.wakeUp();
        }
    }

    public void markAwake(final EngineWorker worker) {
        sleepingWorkers.remove(worker);
    }

    public long getNumberOfConnectedWorkers() {
        return numberOfConnectedWorkers.longValue();
    }
}

@@ -98,34 +98,56 @@
 Worker;%0A
-%0A
 import 
-java.util.
+org.eclipse.jetty.util.Concurrent
 HashSet;
@@ -147,16 +147,17 @@
 ashSet;%0A
+%0A
 import j
@@ -232,32 +232,42 @@
   private final 
+Concurrent
 HashSet%3CEngineWo
@@ -307,16 +307,26 @@
 e final 
+Concurrent
 HashSet%3C
@@ -475,32 +475,42 @@
 ngWorkers = new 
+Concurrent
 HashSet%3C%3E();%0A   
@@ -534,16 +534,26 @@
 s = new 
+Concurrent
 HashSet%3C

Make WorkerPool use a ConcurrentHashSet (backed by CHM)