use of java.util.concurrent.ThreadFactory in project imcache by Cetsoft.
the class ConcurrentEvictionListener method init.
/**
* Inits the.
*
* @param queueSize the queue size
* @param concurrencyLevel the concurrency level
*/
protected void init(int queueSize, int concurrencyLevel) {
cacheTasks = new ArrayBlockingQueue<CacheTask<K, V>>(queueSize);
ExecutorService drainerService = Executors.newFixedThreadPool(concurrencyLevel, new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return ThreadUtils.createDaemonThread(runnable, "imcache:concurrentAsyncEvictionDrainer(thread=" + NO_OF_EVICTION_DRAINERS.incrementAndGet() + ")");
}
});
// Creates runnables to drain cache task queue constantly.
for (int i = 0; i < concurrencyLevel; i++) {
drainerService.execute(new Runnable() {
public void run() {
while (true) {
drainQueue();
}
}
});
}
}
use of java.util.concurrent.ThreadFactory in project pulsar by yahoo.
the class MockBrokerService method startMockBrokerService.
public void startMockBrokerService() throws Exception {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
final int numThreads = 2;
final int MaxMessageSize = 5 * 1024 * 1024;
EventLoopGroup eventLoopGroup;
try {
if (SystemUtils.IS_OS_LINUX) {
try {
eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory);
} catch (UnsatisfiedLinkError e) {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
} else {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
workerGroup = eventLoopGroup;
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup, workerGroup);
if (workerGroup instanceof EpollEventLoopGroup) {
bootstrap.channel(EpollServerSocketChannel.class);
} else {
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
ch.pipeline().addLast("handler", new MockServerCnx());
}
});
// Bind and start to accept incoming connections.
bootstrap.bind(brokerServicePort).sync();
} catch (Exception e) {
throw e;
}
}
use of java.util.concurrent.ThreadFactory in project jdk8u_jdk by JetBrains.
the class AsyncCloseAndInterrupt method main.
public static void main(String[] args) throws Exception {
wildcardAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
initAcceptor();
if (!TestUtil.onWindows())
initRefuser();
initPipes();
initFile();
if (TestUtil.onWindows()) {
log.println("WARNING: Cannot test FileChannel transfer operations" + " on Windows");
} else {
test(diskFileChannelFactory, TRANSFER_TO);
test(diskFileChannelFactory, TRANSFER_FROM);
}
if (fifoFile != null)
test(fifoFileChannelFactory);
// Testing positional file reads and writes is impractical: It requires
// access to a large file soft-mounted via NFS, and even then isn't
// completely guaranteed to work.
//
// Testing map is impractical and arguably unnecessary: It's
// unclear under what conditions mmap(2) will actually block.
test(connectedSocketChannelFactory);
if (TestUtil.onWindows()) {
log.println("WARNING Cannot reliably test connect/finishConnect" + " operations on Windows");
} else {
// Only the following tests need refuser's connection backlog
// to be saturated
ExecutorService pumperExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName("Pumper");
return t;
}
});
pumpDone = false;
try {
Future<Integer> pumpFuture = pumpRefuser(pumperExecutor);
waitPump("\nWait for initial Pump");
test(socketChannelFactory, CONNECT);
test(socketChannelFactory, FINISH_CONNECT);
pumpDone = true;
Integer newConn = pumpFuture.get(30, TimeUnit.SECONDS);
log.println("Pump " + newConn + " connections.");
} finally {
pumperExecutor.shutdown();
}
}
test(serverSocketChannelFactory, ACCEPT);
test(datagramChannelFactory);
test(pipeSourceChannelFactory);
test(pipeSinkChannelFactory);
}
use of java.util.concurrent.ThreadFactory in project jdk8u_jdk by JetBrains.
the class Leaky method main.
public static void main(String[] args) throws Exception {
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
};
AsynchronousChannelGroup group = AsynchronousChannelGroup.withFixedThreadPool(4, threadFactory);
final int CONNECTION_COUNT = 10;
Connection[] connections = new Connection[CONNECTION_COUNT];
for (int i = 0; i < CONNECTION_COUNT; i++) {
connections[i] = new Connection(group);
}
for (int i = 0; i < 1024; i++) {
// initiate reads
for (Connection conn : connections) {
conn.startRead();
}
// write data so that the read can complete
for (Connection conn : connections) {
conn.write();
}
// complete read
for (Connection conn : connections) {
conn.finishRead();
}
}
// print summary of buffer pool usage
List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
for (BufferPoolMXBean pool : pools) System.out.format(" %8s ", pool.getName());
System.out.println();
for (int i = 0; i < pools.size(); i++) System.out.format("%6s %10s %10s ", "Count", "Capacity", "Memory");
System.out.println();
for (BufferPoolMXBean pool : pools) {
System.out.format("%6d %10d %10d ", pool.getCount(), pool.getTotalCapacity(), pool.getMemoryUsed());
}
System.out.println();
}
use of java.util.concurrent.ThreadFactory in project jersey by jersey.
the class ThreadFactoryBuilder method build.
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final String nameFormat = builder.nameFormat;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
final ThreadFactory backingThreadFactory = (builder.backingThreadFactory != null) ? builder.backingThreadFactory : Executors.defaultThreadFactory();
final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
if (nameFormat != null) {
thread.setName(String.format(nameFormat, count.getAndIncrement()));
}
if (daemon != null) {
thread.setDaemon(daemon);
}
if (priority != null) {
thread.setPriority(priority);
}
if (uncaughtExceptionHandler != null) {
thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
}
return thread;
}
};
}
Aggregations