@@ -0,0 +1,1560 @@
+package net.openhft.chronicle.core.util;%0A%0Aimport net.openhft.affinity.Affinity;%0Aimport net.openhft.chronicle.core.Jvm;%0Aimport net.openhft.chronicle.core.values.IntArrayValues;%0A%0Aimport java.util.concurrent.ThreadLocalRandom;%0A%0Apublic class ThreadIndexAssigner %7B%0A    private final IntArrayValues values;%0A%0A    public ThreadIndexAssigner(IntArrayValues values) %7B%0A        this.values = values;%0A    %7D%0A%0A    public int getId() throws IllegalStateException %7B%0A        int threadId = Affinity.getThreadId();%0A        int size = (int) values.getCapacity();%0A        values.setMaxUsed(size);%0A        // already assigned?%0A        for (int i = 0; i %3C size; i++) %7B%0A            int value = values.getVolatileValueAt(i);%0A            if (value == threadId)%0A                return i;%0A        %7D%0A%0A        int index = nextIndex(size);%0A        for (int i = 0; i %3C size * 2; i++) %7B%0A            int value = values.getVolatileValueAt(index);%0A            boolean processAlive = Jvm.isProcessAlive(value);%0A            System.out.println(%22index: %22 + index + %22, value: %22 + value + %22, alive: %22 + processAlive);%0A            if (value == 0 %7C%7C !processAlive)%0A                if (values.compareAndSet(index, value, threadId))%0A                    return index;%0A            index++;%0A            if (index %3E= size) %7B%0A                Thread.yield();%0A                index = 0;%0A            %7D%0A        %7D%0A        throw new IllegalStateException(%22Unable to acquire an id as all ids are taken%22);%0A    %7D%0A%0A    protected int nextIndex(int size) %7B%0A        return ThreadLocalRandom.current().nextInt(size);%0A    %7D%0A%7D%0A

Add support for a ThreadIndexAssigner for assigning threads to a small number of ids, closes https://github.com/OpenHFT/Chronicle-Core/issues/141