[#2675] Replace synchronization performed on util.concurrent instance in TrafficCounter
Motivation: Message from FindBugs: This method performs synchronization an object that is an instance of a class from the java.util.concurrent package (or its subclasses). Instances of these classes have their own concurrency control mechanisms that are orthogonal to the synchronization provided by the Java keyword synchronized. For example, synchronizing on an AtomicBoolean will not prevent other threads from modifying the AtomicBoolean. Such code may be correct, but should be carefully reviewed and documented, and may confuse people who have to maintain the code at a later date. Modification: Use synchronized(this) Result: Less confusing code
This commit is contained in:
parent
2e2abb0b74
commit
c85319213a
@ -163,8 +163,7 @@ public class TrafficCounter {
|
||||
/**
|
||||
* Start the monitoring process
|
||||
*/
|
||||
public void start() {
|
||||
synchronized (lastTime) {
|
||||
public synchronized void start() {
|
||||
if (monitorActive.get()) {
|
||||
return;
|
||||
}
|
||||
@ -176,13 +175,11 @@ public class TrafficCounter {
|
||||
executor.schedule(monitor, checkInterval.get(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the monitoring process
|
||||
*/
|
||||
public void stop() {
|
||||
synchronized (lastTime) {
|
||||
public synchronized void stop() {
|
||||
if (!monitorActive.get()) {
|
||||
return;
|
||||
}
|
||||
@ -195,15 +192,13 @@ public class TrafficCounter {
|
||||
scheduledFuture.cancel(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the accounting on Read and Write
|
||||
*
|
||||
* @param newLastTime the millisecond unix timestamp that we should be considered up-to-date for
|
||||
*/
|
||||
void resetAccounting(long newLastTime) {
|
||||
synchronized (lastTime) {
|
||||
synchronized void resetAccounting(long newLastTime) {
|
||||
long interval = newLastTime - lastTime.getAndSet(newLastTime);
|
||||
if (interval == 0) {
|
||||
// nothing to do
|
||||
@ -216,7 +211,6 @@ public class TrafficCounter {
|
||||
lastWriteThroughput = lastWrittenBytes / interval * 1000;
|
||||
// nb byte / checkInterval in ms * 1000 (1s)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with the {@link AbstractTrafficShapingHandler} that hosts it, the Timer to use, its
|
||||
|
Loading…
Reference in New Issue
Block a user