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:
parent
c7004ed142
commit
42abb6df3a
@ -46,7 +46,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
public class HttpClientCodec extends CombinedChannelHandler {
|
public class HttpClientCodec extends CombinedChannelHandler {
|
||||||
|
|
||||||
/** A queue that is used for correlating a request and a response. */
|
/** 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) */
|
/** If true, decoding stops (i.e. pass-through) */
|
||||||
volatile boolean done;
|
volatile boolean done;
|
||||||
|
@ -49,7 +49,7 @@ import java.util.Queue;
|
|||||||
*/
|
*/
|
||||||
public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpMessage, HttpMessage, Object, Object> {
|
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;
|
private volatile EncoderEmbedder<ChannelBuffer> encoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,7 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.util.internal;
|
package io.netty.util.internal;
|
||||||
|
|
||||||
import java.util.Collection;
|
import io.netty.logging.InternalLogger;
|
||||||
|
import io.netty.logging.InternalLoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,39 +26,44 @@ import java.util.concurrent.BlockingQueue;
|
|||||||
*/
|
*/
|
||||||
public final class QueueFactory {
|
public final class QueueFactory {
|
||||||
|
|
||||||
private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
|
private static final InternalLogger logger =
|
||||||
|
InternalLoggerFactory.getInstance(QueueFactory.class);
|
||||||
|
|
||||||
private QueueFactory() {
|
private static final boolean USE_LTQ;
|
||||||
// only use static methods!
|
|
||||||
|
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}
|
* Create a new unbound {@link BlockingQueue}
|
||||||
*
|
*
|
||||||
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
|
|
||||||
* @return queue the {@link BlockingQueue} implementation
|
* @return queue the {@link BlockingQueue} implementation
|
||||||
*/
|
*/
|
||||||
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
|
public static <T> BlockingQueue<T> createQueue() {
|
||||||
if (useUnsafe) {
|
if (USE_LTQ) {
|
||||||
return new LinkedTransferQueue<T>();
|
return new LinkedTransferQueue<T>();
|
||||||
} else {
|
} else {
|
||||||
return new LegacyLinkedTransferQueue<T>();
|
return new LegacyLinkedTransferQueue<T>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private QueueFactory() {
|
||||||
* Create a new unbound {@link BlockingQueue}
|
// only use static methods!
|
||||||
*
|
|
||||||
* @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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ public abstract class SingleThreadEventLoop extends AbstractExecutorService impl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fields for event loop
|
// 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 Thread thread;
|
||||||
private final Object stateLock = new Object();
|
private final Object stateLock = new Object();
|
||||||
private final Semaphore threadLock = new Semaphore(0);
|
private final Semaphore threadLock = new Semaphore(0);
|
||||||
|
@ -29,8 +29,7 @@ public class OioEventLoop implements EventLoop {
|
|||||||
final ThreadFactory threadFactory;
|
final ThreadFactory threadFactory;
|
||||||
final Set<OioChildEventLoop> activeChildren = Collections.newSetFromMap(
|
final Set<OioChildEventLoop> activeChildren = Collections.newSetFromMap(
|
||||||
new ConcurrentHashMap<OioChildEventLoop, Boolean>());
|
new ConcurrentHashMap<OioChildEventLoop, Boolean>());
|
||||||
final Queue<OioChildEventLoop> idleChildren =
|
final Queue<OioChildEventLoop> idleChildren = QueueFactory.createQueue();
|
||||||
QueueFactory.createQueue(OioChildEventLoop.class);
|
|
||||||
private final ChannelException tooManyChannels;
|
private final ChannelException tooManyChannels;
|
||||||
|
|
||||||
public OioEventLoop() {
|
public OioEventLoop() {
|
||||||
|
Loading…
Reference in New Issue
Block a user