QueueFactory cleanup

- Really attempt to create a queue to determine LTQ can be initialized
  in runtime, and cache the result
- Remove unnecessary Class<T> parameter in createQueue()
- Remove unused createQueue(Collection)
This commit is contained in:
Trustin Lee 2012-05-30 16:19:22 -07:00
parent c7004ed142
commit 42abb6df3a
5 changed files with 38 additions and 32 deletions

View File

@ -46,7 +46,7 @@ import java.util.concurrent.atomic.AtomicLong;
public class HttpClientCodec extends CombinedChannelHandler {
/** A queue that is used for correlating a request and a response. */
final Queue<HttpMethod> queue = QueueFactory.createQueue(HttpMethod.class);
final Queue<HttpMethod> queue = QueueFactory.createQueue();
/** If true, decoding stops (i.e. pass-through) */
volatile boolean done;

View File

@ -49,7 +49,7 @@ import java.util.Queue;
*/
public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpMessage, HttpMessage, Object, Object> {
private final Queue<String> acceptEncodingQueue = QueueFactory.createQueue(String.class);
private final Queue<String> acceptEncodingQueue = QueueFactory.createQueue();
private volatile EncoderEmbedder<ChannelBuffer> encoder;
/**

View File

@ -15,7 +15,9 @@
*/
package io.netty.util.internal;
import java.util.Collection;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import java.util.concurrent.BlockingQueue;
/**
@ -23,40 +25,45 @@ import java.util.concurrent.BlockingQueue;
* instance for the running JVM.
*/
public final class QueueFactory {
private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
private QueueFactory() {
// only use static methods!
private static final InternalLogger logger =
InternalLoggerFactory.getInstance(QueueFactory.class);
private static final boolean USE_LTQ;
static {
boolean useLTQ = false;
try {
if (DetectionUtil.hasUnsafe()) {
new LinkedTransferQueue<Object>();
useLTQ = true;
}
logger.debug(
"No access to the Unsafe - using " +
LegacyLinkedTransferQueue.class.getSimpleName() + " instead.");
} catch (Throwable t) {
logger.debug(
"Failed to initialize a " + LinkedTransferQueue.class.getSimpleName() + " - " +
"using " + LegacyLinkedTransferQueue.class.getSimpleName() + " instead.", t);
}
USE_LTQ = useLTQ;
}
/**
* Create a new unbound {@link BlockingQueue}
*
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
* Create a new unbound {@link BlockingQueue}
*
* @return queue the {@link BlockingQueue} implementation
*/
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
if (useUnsafe) {
public static <T> BlockingQueue<T> createQueue() {
if (USE_LTQ) {
return new LinkedTransferQueue<T>();
} else {
return new LegacyLinkedTransferQueue<T>();
}
}
/**
* Create a new unbound {@link BlockingQueue}
*
* @param collection the collection which should get copied to the newly created {@link BlockingQueue}
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
* @return queue the {@link BlockingQueue} implementation
*/
public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
if (useUnsafe) {
return new LinkedTransferQueue<T>(collection);
} else {
return new LegacyLinkedTransferQueue<T>(collection);
}
private QueueFactory() {
// only use static methods!
}
}

View File

@ -50,7 +50,7 @@ public abstract class SingleThreadEventLoop extends AbstractExecutorService impl
}
// Fields for event loop
private final BlockingQueue<Runnable> taskQueue = QueueFactory.createQueue(Runnable.class);
private final BlockingQueue<Runnable> taskQueue = QueueFactory.createQueue();
private final Thread thread;
private final Object stateLock = new Object();
private final Semaphore threadLock = new Semaphore(0);

View File

@ -29,8 +29,7 @@ public class OioEventLoop implements EventLoop {
final ThreadFactory threadFactory;
final Set<OioChildEventLoop> activeChildren = Collections.newSetFromMap(
new ConcurrentHashMap<OioChildEventLoop, Boolean>());
final Queue<OioChildEventLoop> idleChildren =
QueueFactory.createQueue(OioChildEventLoop.class);
final Queue<OioChildEventLoop> idleChildren = QueueFactory.createQueue();
private final ChannelException tooManyChannels;
public OioEventLoop() {