use of java.util.concurrent.ExecutorService in project mapdb by jankotek.
the class LinkedTransferQueueTest method testOfferInExecutor.
/**
* offer transfers elements across Executor tasks
*/
public void testOfferInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try (PoolCleaner cleaner = cleaner(executor)) {
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
long startTime = System.nanoTime();
assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
assertSame(one, q.take());
checkEmpty(q);
}
});
}
}
use of java.util.concurrent.ExecutorService in project mapdb by jankotek.
the class LinkedBlockingQueueTest method testPollInExecutor.
/**
* timed poll retrieves elements across Executor threads
*/
public void testPollInExecutor() {
final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try (PoolCleaner cleaner = cleaner(executor)) {
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertNull(q.poll());
threadsStarted.await();
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
checkEmpty(q);
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
q.put(one);
}
});
}
}
use of java.util.concurrent.ExecutorService in project mapdb by jankotek.
the class LinkedTransferQueueTest method testPollInExecutor.
/**
* timed poll retrieves elements across Executor threads
*/
public void testPollInExecutor() {
final LinkedTransferQueue q = new LinkedTransferQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try (PoolCleaner cleaner = cleaner(executor)) {
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertNull(q.poll());
threadsStarted.await();
long startTime = System.nanoTime();
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
checkEmpty(q);
}
});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
q.put(one);
}
});
}
}
use of java.util.concurrent.ExecutorService in project jna by java-native-access.
the class Kernel32NamedPipeTest method testMultiThreadedNamedPipe.
@Test
public void testMultiThreadedNamedPipe() {
final String pipeName = "\\\\.\\pipe\\" + getCurrentTestName();
final Logger logger = Logger.getLogger(getClass().getName());
final int MAX_BUFFER_SIZE = 1024;
ExecutorService executors = Executors.newFixedThreadPool(2);
try {
Future<?> server = executors.submit(new Runnable() {
@Override
public void run() {
// based on https://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx
HANDLE hNamedPipe = assertValidHandle("CreateNamedPipe", Kernel32.INSTANCE.CreateNamedPipe(pipeName, // dwOpenMode
WinBase.PIPE_ACCESS_DUPLEX, // dwPipeMode
WinBase.PIPE_TYPE_MESSAGE | WinBase.PIPE_READMODE_MESSAGE | WinBase.PIPE_WAIT, // nMaxInstances,
1, // nOutBufferSize,
MAX_BUFFER_SIZE, // nInBufferSize,
MAX_BUFFER_SIZE, // nDefaultTimeOut,
(int) TimeUnit.SECONDS.toMillis(30L), // lpSecurityAttributes
null));
try {
logger.info("Await client connection");
assertCallSucceeded("ConnectNamedPipe", Kernel32.INSTANCE.ConnectNamedPipe(hNamedPipe, null));
logger.info("Client connected");
byte[] readBuffer = new byte[MAX_BUFFER_SIZE];
IntByReference lpNumberOfBytesRead = new IntByReference(0);
assertCallSucceeded("ReadFile", Kernel32.INSTANCE.ReadFile(hNamedPipe, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
int readSize = lpNumberOfBytesRead.getValue();
logger.info("Received client data - length=" + readSize);
assertTrue("No data receieved from client", readSize > 0);
IntByReference lpNumberOfBytesWritten = new IntByReference(0);
assertCallSucceeded("WriteFile", Kernel32.INSTANCE.WriteFile(hNamedPipe, readBuffer, readSize, lpNumberOfBytesWritten, null));
logger.info("Echoed client data - length=" + lpNumberOfBytesWritten.getValue());
assertEquals("Mismatched write buffer size", readSize, lpNumberOfBytesWritten.getValue());
// Flush the pipe to allow the client to read the pipe's contents before disconnecting
assertCallSucceeded("FlushFileBuffers", Kernel32.INSTANCE.FlushFileBuffers(hNamedPipe));
logger.info("Disconnecting");
assertCallSucceeded("DisconnectNamedPipe", Kernel32.INSTANCE.DisconnectNamedPipe(hNamedPipe));
logger.info("Disconnected");
} finally {
// clean up
assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hNamedPipe));
}
}
});
logger.info("Started server - handle=" + server);
Future<?> client = executors.submit(new Runnable() {
@Override
public void run() {
// based on https://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx
assertCallSucceeded("WaitNamedPipe", Kernel32.INSTANCE.WaitNamedPipe(pipeName, (int) TimeUnit.SECONDS.toMillis(15L)));
logger.info("Connected to server");
HANDLE hPipe = assertValidHandle("CreateNamedPipe", Kernel32.INSTANCE.CreateFile(pipeName, WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, // no sharing
0, // default security attributes
null, // opens existing pipe
WinNT.OPEN_EXISTING, // default attributes
0, // no template file
null));
try {
IntByReference lpMode = new IntByReference(WinBase.PIPE_READMODE_MESSAGE);
assertCallSucceeded("SetNamedPipeHandleState", Kernel32.INSTANCE.SetNamedPipeHandleState(hPipe, lpMode, null, null));
String expMessage = Thread.currentThread().getName() + " says hello";
byte[] expData = expMessage.getBytes();
IntByReference lpNumberOfBytesWritten = new IntByReference(0);
assertCallSucceeded("WriteFile", Kernel32.INSTANCE.WriteFile(hPipe, expData, expData.length, lpNumberOfBytesWritten, null));
logger.info("Sent hello message");
assertEquals("Mismatched write buffer size", expData.length, lpNumberOfBytesWritten.getValue());
byte[] readBuffer = new byte[MAX_BUFFER_SIZE];
IntByReference lpNumberOfBytesRead = new IntByReference(0);
assertCallSucceeded("ReadFile", Kernel32.INSTANCE.ReadFile(hPipe, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
int readSize = lpNumberOfBytesRead.getValue();
logger.info("Received server data - length=" + readSize);
assertTrue("No data receieved from server", readSize > 0);
String actMessage = new String(readBuffer, 0, readSize);
assertEquals("Mismatched server data", expMessage, actMessage);
} finally {
// clean up
assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hPipe));
}
}
});
logger.info("Started client - handle=" + client);
for (Future<?> f : Arrays.asList(client, server)) {
try {
f.get(30L, TimeUnit.SECONDS);
logger.info("Finished " + f);
} catch (Exception e) {
logger.warning(e.getClass().getSimpleName() + " while await completion of " + f + ": " + e.getMessage());
}
}
} finally {
executors.shutdownNow();
}
}
use of java.util.concurrent.ExecutorService in project hadoop by apache.
the class TestLogAggregationService method testFixedSizeThreadPool.
@Test(timeout = 30000)
public void testFixedSizeThreadPool() throws Exception {
// store configured thread pool size temporarily for restoration
int initThreadPoolSize = conf.getInt(YarnConfiguration.NM_LOG_AGGREGATION_THREAD_POOL_SIZE, YarnConfiguration.DEFAULT_NM_LOG_AGGREGATION_THREAD_POOL_SIZE);
int threadPoolSize = 3;
conf.setInt(YarnConfiguration.NM_LOG_AGGREGATION_THREAD_POOL_SIZE, threadPoolSize);
DeletionService delSrvc = mock(DeletionService.class);
LocalDirsHandlerService dirSvc = mock(LocalDirsHandlerService.class);
when(dirSvc.getLogDirs()).thenThrow(new RuntimeException());
LogAggregationService logAggregationService = new LogAggregationService(dispatcher, this.context, delSrvc, dirSvc);
logAggregationService.init(this.conf);
logAggregationService.start();
ExecutorService executorService = logAggregationService.threadPool;
// used to block threads in the thread pool because main thread always
// acquires the write lock first.
final ReadWriteLock rwLock = new ReentrantReadWriteLock();
final Lock rLock = rwLock.readLock();
final Lock wLock = rwLock.writeLock();
try {
wLock.lock();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
// threads in the thread pool running this will be blocked
rLock.tryLock(35000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rLock.unlock();
}
}
};
// created in the thread pool, each of which is blocked on the read lock.
for (int i = 0; i < threadPoolSize + 1; i++) {
executorService.submit(runnable);
}
// count the number of current running LogAggregationService threads
int runningThread = ((ThreadPoolExecutor) executorService).getActiveCount();
assertEquals(threadPoolSize, runningThread);
} finally {
wLock.unlock();
}
logAggregationService.stop();
logAggregationService.close();
// restore the original configurations to avoid side effects
conf.setInt(YarnConfiguration.NM_LOG_AGGREGATION_THREAD_POOL_SIZE, initThreadPoolSize);
}
Aggregations