sclass FastCollab is ICollab { settable bool useMainThread = true; volatile settable bool done; int coresToUse; FastCollabWorker[] workers; AtomicReferenceArray workArray; *(int coresToUse) { this.coresToUse = max(1, coresToUse); workArray = new AtomicReferenceArray(this.coresToUse); } public void addWork(Runnable work) { if (work == null) ret; for (int i = 0; i < workArray.length(); i++) if (workArray.compareAndSet(i, null, work)) ret; // Everyone is busy - do the work ourselves work.run(); } // returns null => sleep // returns done => all work done public Runnable grabWork(int workerIndex) { if (done) ret DONE; Runnable work = workArray.get(workerIndex); if (work != null) workArray.set(workerIndex, null); ret work; } toString { ret "FastCollab*" + n2(coresToUse); } run { workers = new FastCollabWorker[coresToUse]; for i to coresToUse: workers[i] = new FastCollabWorker(this, i); for (int i = useMainThread ? 1 : 0; i < coresToUse; i++) startThread(workers[i]); if (useMainThread) workers[0].run(); } }