Use AtomicInteger instead of local synchronization around int field
Issue: SPR-11103
This commit is contained in:
parent
8a6b095204
commit
1cd5071e6a
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.util;
|
package org.springframework.util;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple customizable helper class for creating threads. Provides various
|
* Simple customizable helper class for creating threads. Provides various
|
||||||
|
|
@ -40,9 +41,7 @@ public class CustomizableThreadCreator implements Serializable {
|
||||||
|
|
||||||
private ThreadGroup threadGroup;
|
private ThreadGroup threadGroup;
|
||||||
|
|
||||||
private int threadCount = 0;
|
private final AtomicInteger threadCount = new AtomicInteger();
|
||||||
|
|
||||||
private final Object threadCountMonitor = new SerializableMonitor();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -161,12 +160,7 @@ public class CustomizableThreadCreator implements Serializable {
|
||||||
* @see #getThreadNamePrefix()
|
* @see #getThreadNamePrefix()
|
||||||
*/
|
*/
|
||||||
protected String nextThreadName() {
|
protected String nextThreadName() {
|
||||||
int threadNumber = 0;
|
return getThreadNamePrefix() + this.threadCount.incrementAndGet();
|
||||||
synchronized (this.threadCountMonitor) {
|
|
||||||
this.threadCount++;
|
|
||||||
threadNumber = this.threadCount;
|
|
||||||
}
|
|
||||||
return getThreadNamePrefix() + threadNumber;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -177,11 +171,4 @@ public class CustomizableThreadCreator implements Serializable {
|
||||||
return ClassUtils.getShortName(getClass()) + "-";
|
return ClassUtils.getShortName(getClass()) + "-";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Empty class used for a serializable monitor object.
|
|
||||||
*/
|
|
||||||
private static class SerializableMonitor implements Serializable {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue