Code example for AtomicInteger

Methods: getAndIncrement

0
    public static ThreadFactory newNonDaemonThreadFactory(final String name) {
        final AtomicInteger counter = new AtomicInteger();
        return new ThreadFactory() {
            @Override 
            public Thread newThread(final Runnable run) {
                return new Thread(run, name + '-' + counter.getAndIncrement());
            } 
        }; 
    } 
     
    public static ThreadFactory newDaemonThreadFactory(final String name) {
        final AtomicInteger counter = new AtomicInteger();
        return new ThreadFactory() {
            @Override 
            public Thread newThread(final Runnable run) {
                final Thread t =
                    new Thread(run, name + '-' + counter.getAndIncrement());
                t.setDaemon(true);
                return t;
            } 
        };