提交 1cd5071e 编写于 作者: J Juergen Hoeller

Use AtomicInteger instead of local synchronization around int field

Issue: SPR-11103
上级 8a6b0952
/* /*
* 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 @@ ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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 {
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册