From 93c990dd7a2c4d3b839d38524c89e06e812e6dac Mon Sep 17 00:00:00 2001 From: Cruz Julian Bishop Date: Sat, 25 Aug 2012 13:43:02 +1000 Subject: [PATCH] Generate Channel IDs in a pseudorandom fashion Requested by @psweeny in #547 Signed-off-by: Cruz Julian Bishop --- .../org/jboss/netty/channel/AbstractChannel.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/AbstractChannel.java b/src/main/java/org/jboss/netty/channel/AbstractChannel.java index 6de954403e..50ffb17826 100644 --- a/src/main/java/org/jboss/netty/channel/AbstractChannel.java +++ b/src/main/java/org/jboss/netty/channel/AbstractChannel.java @@ -16,6 +16,7 @@ package org.jboss.netty.channel; import java.net.SocketAddress; +import java.util.Random; import java.util.concurrent.ConcurrentMap; import org.jboss.netty.util.internal.ConcurrentHashMap; @@ -27,8 +28,14 @@ public abstract class AbstractChannel implements Channel { static final ConcurrentMap allChannels = new ConcurrentHashMap(); + private static final Random random = new Random(); + + public static Random getRandom() { + return random; + } + private static Integer allocateId(Channel channel) { - Integer id = System.identityHashCode(channel); + Integer id = random.nextInt(); for (;;) { // Loop until a unique ID is acquired. // It should be found in one loop practically. @@ -139,12 +146,11 @@ public abstract class AbstractChannel implements Channel { } /** - * Returns the {@linkplain System#identityHashCode(Object) identity hash code} - * of this channel. + * Returns the ID of this channel. */ @Override public final int hashCode() { - return System.identityHashCode(this); + return this.id; } /**