[concurrency-interest] Concurrent telemetry gathering

Gregg Wonderly gregg at cytetech.com
Wed Nov 15 15:11:13 EST 2006


I'm working on collecting telemetry regarding bandwidth utilization through a 
"connection".  I've defined the following class, thinking that this should 
minimize concurrency for updating the numbers.

In addition, there is a 
ConcurrentHashMap<OutboundConnection,ConnectionBandwidth> object that is 
accessed by threads of execution which are handling the I/O requests through the 
system.  That map is lazily initialized with putIfAbsent().  When the first 
entry is added, there is a ScheduledThreadPoolExecutor used to schedule a 
regular execution of "run" to create a time based running average of bandwidth 
usage.

Is there anything else I should do differently?

public class ConnectionBandwidth implements Runnable, Serializable {
	private double recv, xmit;
	private AtomicLong crecv = new AtomicLong(), cxmit = new AtomicLong();
	private transient OutboundConnection conn;
	private double avgIntv, avgIntvMinusOne;

	public ConnectionBandwidth( OutboundConnection conn, int intv ) {
		this.conn = conn;
		avgIntv = intv;
		avgIntvMinusOne = intv - 1;
	}
	
	public synchronized void setAverageInterval( int intv ) {
		avgIntv = intv;
		avgIntvMinusOne = intv - 1;
	}

	public void bytesRecv( long val ) {
		crecv.addAndGet( val );
	}

	public void bytesXmit( long val ) {
		cxmit.addAndGet( val );
	}

	public final synchronized void run() {
		recv = ( (recv * avgIntvMinusOne ) + crecv.getAndSet(0) ) / avgIntv;
		xmit = ( (xmit * avgIntvMinusOne ) + cxmit.getAndSet(0) ) / avgIntv;
	}
}

Gregg Wonderly


More information about the Concurrency-interest mailing list