Use AtomicInteger instead of local synchronization around int field

Issue: SPR-11103
This commit is contained in:
Juergen Hoeller 2014-02-04 17:45:03 +01:00
parent 8a6b095204
commit 1cd5071e6a
1 changed files with 4 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.util;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Simple customizable helper class for creating threads. Provides various
@ -40,9 +41,7 @@ public class CustomizableThreadCreator implements Serializable {
private ThreadGroup threadGroup;
private int threadCount = 0;
private final Object threadCountMonitor = new SerializableMonitor();
private final AtomicInteger threadCount = new AtomicInteger();
/**
@ -161,12 +160,7 @@ public class CustomizableThreadCreator implements Serializable {
* @see #getThreadNamePrefix()
*/
protected String nextThreadName() {
int threadNumber = 0;
synchronized (this.threadCountMonitor) {
this.threadCount++;
threadNumber = this.threadCount;
}
return getThreadNamePrefix() + threadNumber;
return getThreadNamePrefix() + this.threadCount.incrementAndGet();
}
/**
@ -177,11 +171,4 @@ public class CustomizableThreadCreator implements Serializable {
return ClassUtils.getShortName(getClass()) + "-";
}
/**
* Empty class used for a serializable monitor object.
*/
private static class SerializableMonitor implements Serializable {
}
}