package org.webbitserver.helpers;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * Wraps a ThreadFactory and assigns a name to each newly created thread, that includes two
 * counters: one to indicate which ThreadFactory instance this is, and another for the thread
 * created by the factory.
 */
public class NamingThreadFactory implements ThreadFactory {
    private static volatile int factoryCount;
    private volatile int threadCount;

    private final ThreadFactory factory;
    private final String prefix;

    public NamingThreadFactory(ThreadFactory factory, String prefix) {
        this.factory = factory;
        this.prefix = prefix;
        factoryCount++;
    }

    public NamingThreadFactory(String prefix) {
        this(Executors.defaultThreadFactory(), prefix);
    }

    @Override
    public Thread newThread(Runnable r) {
        this.threadCount++;
        Thread thread = factory.newThread(r);
        thread.setName(threadName());
        return thread;
    }

    /**
     * Override this method to customize thread name.
     */
    protected String threadName() {
        return String.format("%s-%d-%d-thread", prefix, factoryCount, threadCount);
    }
}
@@ -109,16 +109,66 @@
 Factory;
+%0Aimport java.util.concurrent.atomic.AtomicInteger;
 %0A%0A/**%0A *
@@ -465,71 +465,129 @@
 tic 
-volatile int factoryCount;%0A    private volatile int threadCount
+final AtomicInteger factoryCount = new AtomicInteger();%0A    private final AtomicInteger threadCount = new AtomicInteger()
 ;%0A%0A 
@@ -809,26 +809,42 @@
 factoryCount
-++
+.incrementAndGet()
 ;%0A    %7D%0A%0A   
@@ -1015,21 +1015,16 @@
         
-this.
 threadCo
@@ -1030,10 +1030,26 @@
 ount
-++
+.incrementAndGet()
 ;%0A  
@@ -1334,16 +1334,27 @@
 oryCount
+.intValue()
 , thread
@@ -1358,16 +1358,27 @@
 eadCount
+.intValue()
 );%0A    %7D

Use AtomicInteger instead of volatile int. For #95.