java.util.concurrent.SynchronousQueue Java Examples
The following examples show how to use
java.util.concurrent.SynchronousQueue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SynchronousQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * drainTo(c, n) empties up to n elements of queue into c */ public void testDrainToN() throws InterruptedException { final SynchronousQueue q = new SynchronousQueue(); Thread t1 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(one); }}); Thread t2 = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(two); }}); ArrayList l = new ArrayList(); int drained; while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); assertEquals(1, drained); assertEquals(1, l.size()); while ((drained = q.drainTo(l, 1)) == 0) Thread.yield(); assertEquals(1, drained); assertEquals(2, l.size()); assertTrue(l.contains(one)); assertTrue(l.contains(two)); awaitTermination(t1); awaitTermination(t2); }
Example #2
Source File: SynchronousQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * a deserialized serialized queue is usable */ public void testSerialization() { final SynchronousQueue x = new SynchronousQueue(); final SynchronousQueue y = new SynchronousQueue(false); final SynchronousQueue z = new SynchronousQueue(true); assertSerialEquals(x, y); assertNotSerialEquals(x, z); SynchronousQueue[] qs = { x, y, z }; for (SynchronousQueue q : qs) { SynchronousQueue clone = serialClone(q); assertNotSame(q, clone); assertSerialEquals(q, clone); assertTrue(clone.isEmpty()); assertEquals(0, clone.size()); assertEquals(0, clone.remainingCapacity()); assertFalse(clone.offer(zero)); } }
Example #3
Source File: ThreadPoolFactory.java From Thunder with Apache License 2.0 | 6 votes |
private static BlockingQueue<Runnable> createBlockingQueue() { String queue = properties.getString(ThunderConstant.THREAD_POOL_QUEUE_ATTRIBUTE_NAME); ThreadQueueType queueType = ThreadQueueType.fromString(queue); int queueCapacity = ThunderConstant.CPUS * properties.getInteger(ThunderConstant.THREAD_POOL_QUEUE_CAPACITY_ATTRIBUTE_NAME); switch (queueType) { case LINKED_BLOCKING_QUEUE: return new LinkedBlockingQueue<Runnable>(queueCapacity); case ARRAY_BLOCKING_QUEUE: return new ArrayBlockingQueue<Runnable>(queueCapacity); case SYNCHRONOUS_QUEUE: return new SynchronousQueue<Runnable>(); } return null; }
Example #4
Source File: Util.java From canal with Apache License 2.0 | 6 votes |
public static ThreadPoolExecutor newSingleDaemonThreadExecutor(long keepAliveTime) { return new ThreadPoolExecutor(1, 1, keepAliveTime, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), DaemonThreadFactory.daemonThreadFactory, (r, exe) -> { if (!exe.isShutdown()) { try { exe.getQueue().put(r); } catch (InterruptedException e) { // ignore } } }); }
Example #5
Source File: ThreadUtil.java From Kylin with Apache License 2.0 | 6 votes |
@SuppressWarnings("unused") public static void main(String[] args) { ThreadPoolExecutor pool = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());//Threads.newDaemonThreadFactory("htable")); for (int i = 0; i < Integer.MAX_VALUE; ++i) { System.out.println("index: " + i); Future<?> future = pool.submit(new Runnable() { @Override public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
Example #6
Source File: Util.java From canal with Apache License 2.0 | 6 votes |
public static ThreadPoolExecutor newFixedDaemonThreadPool(int nThreads, long keepAliveTime) { return new ThreadPoolExecutor(nThreads, nThreads, keepAliveTime, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), DaemonThreadFactory.daemonThreadFactory, (r, exe) -> { if (!exe.isShutdown()) { try { exe.getQueue().put(r); } catch (InterruptedException e) { // ignore } } } ); }
Example #7
Source File: EmptyIterator.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void test(String[] args) throws Throwable { testEmptyCollection(emptyList()); testEmptyCollection(emptySet()); testEmptyCollection(new SynchronousQueue<Object>()); testEmptyMap(emptyMap()); Hashtable<?,?> emptyTable = new Hashtable<>(); testEmptyEnumeration(emptyTable.keys()); testEmptyEnumeration(emptyTable.elements()); testEmptyIterator(emptyTable.keySet().iterator()); testEmptyIterator(emptyTable.values().iterator()); testEmptyIterator(emptyTable.entrySet().iterator()); final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration(); testEmptyEnumeration(finalEmptyTyped); final Enumeration<?> finalEmptyAbstract = emptyEnumeration(); testEmptyEnumeration(finalEmptyAbstract); testEmptyIterator(emptyIterator()); }
Example #8
Source File: DefaultGroovyMethodsSupport.java From groovy with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") protected static <T> Queue<T> createSimilarQueue(Queue<T> orig) { if (orig instanceof ArrayBlockingQueue) { ArrayBlockingQueue queue = (ArrayBlockingQueue) orig; return new ArrayBlockingQueue<T>(queue.size() + queue.remainingCapacity()); } else if (orig instanceof ArrayDeque) { return new ArrayDeque<T>(); } else if (orig instanceof ConcurrentLinkedQueue) { return new ConcurrentLinkedQueue<T>(); } else if (orig instanceof DelayQueue) { return new DelayQueue(); } else if (orig instanceof LinkedBlockingDeque) { return new LinkedBlockingDeque<T>(); } else if (orig instanceof LinkedBlockingQueue) { return new LinkedBlockingQueue<T>(); } else if (orig instanceof PriorityBlockingQueue) { return new PriorityBlockingQueue<T>(); } else if (orig instanceof PriorityQueue) { return new PriorityQueue<T>(11, ((PriorityQueue) orig).comparator()); } else if (orig instanceof SynchronousQueue) { return new SynchronousQueue<T>(); } else { return new LinkedList<T>(); } }
Example #9
Source File: ClientThreadPoolsTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Test public void testThreadPoolInjection() throws Exception { ServerLocator serverLocator = new ServerLocatorImpl(false); ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); ScheduledThreadPoolExecutor scheduledThreadPool = new ScheduledThreadPoolExecutor(1); serverLocator.setThreadPools(threadPool, scheduledThreadPool); Field threadPoolField = ServerLocatorImpl.class.getDeclaredField("threadPool"); Field scheduledThreadPoolField = ServerLocatorImpl.class.getDeclaredField("scheduledThreadPool"); serverLocator.initialize(); threadPoolField.setAccessible(true); scheduledThreadPoolField.setAccessible(true); ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadPoolField.get(serverLocator); ScheduledThreadPoolExecutor stpe = (ScheduledThreadPoolExecutor) scheduledThreadPoolField.get(serverLocator); assertEquals(threadPool, tpe); assertEquals(scheduledThreadPool, stpe); }
Example #10
Source File: ExecutorBuilder.java From tomee with Apache License 2.0 | 6 votes |
public BlockingQueue<Runnable> create(final Options options, final String prefix, final int queueSize) { switch (this) { case ARRAY: { return new ArrayBlockingQueue<>(queueSize > 0 ? queueSize : 1); } case LINKED: { return new LinkedBlockingQueue<>(queueSize > 0 ? queueSize : 1); } case PRIORITY: { return new PriorityBlockingQueue<>(); } case SYNCHRONOUS: { return new SynchronousQueue<>(options.get(prefix + ".QueueFair", false)); } default: { // The Options class will throw an error if the user supplies an unknown enum string // The only way we can reach this is if we add a new QueueType element and forget to // implement it in the above switch statement. throw new IllegalArgumentException("Unknown QueueType type: " + this); } } }
Example #11
Source File: SynchronousQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testPollInExecutor(boolean fair) { final SynchronousQueue q = new SynchronousQueue(fair); 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)); assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.await(); q.put(one); }}); } }
Example #12
Source File: GcsOptions.java From beam with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") // IS_APP_ENGINE is deprecated for internal use only. @Override public ExecutorService create(PipelineOptions options) { ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder(); threadFactoryBuilder.setThreadFactory(MoreExecutors.platformThreadFactory()); threadFactoryBuilder.setDaemon(true); /* The SDK requires an unbounded thread pool because a step may create X writers * each requiring their own thread to perform the writes otherwise a writer may * block causing deadlock for the step because the writers buffer is full. * Also, the MapTaskExecutor launches the steps in reverse order and completes * them in forward order thus requiring enough threads so that each step's writers * can be active. */ return new ThreadPoolExecutor( 0, Integer.MAX_VALUE, // Allow an unlimited number of re-usable threads. Long.MAX_VALUE, TimeUnit.NANOSECONDS, // Keep non-core threads alive forever. new SynchronousQueue<>(), threadFactoryBuilder.build()); }
Example #13
Source File: AsyncExecutorServiceImpl.java From ankush with GNU Lesser General Public License v3.0 | 5 votes |
/** * Instantiates a new async executor service impl. */ public AsyncExecutorServiceImpl() { super(MIN_THREAD_POOL_SIZE, MAX_THREAD_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new CustomizableThreadFactory()); ThreadFactory factory = this.getThreadFactory(); if (factory instanceof CustomizableThreadFactory) { CustomizableThreadFactory customizableThreadFactory = (CustomizableThreadFactory) factory; customizableThreadFactory .setThreadNamePrefix("AnkushProgressAwareThread_"); customizableThreadFactory.setDaemon(true); } }
Example #14
Source File: ThreadPoolManager.java From aion-germany with GNU General Public License v3.0 | 5 votes |
/** * Constructor. */ private ThreadPoolManager() { new DeadLockDetector(60, DeadLockDetector.RESTART).start(); scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(4, new PriorityThreadFactory("ScheduledThreadPool", Thread.NORM_PRIORITY)); scheduledThreadPool = MoreExecutors.listeningDecorator(scheduledThreadPoolExecutor); generalPacketsThreadPoolExecutor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); generalPacketsThreadPool = MoreExecutors.listeningDecorator(generalPacketsThreadPoolExecutor); }
Example #15
Source File: PooledExecutorWithDMStats.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Create a new pool that uses the supplied Channel for queuing, and * with all default parameter settings except for pool size. **/ public PooledExecutorWithDMStats(BlockingQueue<Runnable> q, int maxPoolSize, PoolStatHelper stats, ThreadFactory tf, int msTimeout) { this(initQ(q), maxPoolSize, stats, tf, msTimeout, initREH(q)); if (!(q instanceof SynchronousQueue)) { this.bufferQueue = q; // create a thread that takes from bufferQueue and puts into result final BlockingQueue<Runnable> takeQueue = q; final BlockingQueue<Runnable> putQueue = getQueue(); Runnable r = new Runnable() { public void run() { try { for (;;) { SystemFailure.checkFailure(); putQueue.put(takeQueue.take()); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // this thread is being shutdown so just return; return; } } }; this.bufferConsumer = tf.newThread(r); this.bufferConsumer.start(); } }
Example #16
Source File: FunctionExecutionPooledExecutor.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
private static BlockingQueue<Runnable> initQ(BlockingQueue<Runnable> q) { if (q instanceof SynchronousQueue || q instanceof SynchronousQueueNoSpin) { return q; } else { return new SynchronousQueueNoSpin<Runnable>(); } }
Example #17
Source File: IndexManager.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
public IndexManager(IndexServer indexServer, ClusterStatus clusterStatus, BlurFilterCache filterCache, int maxHeapPerRowFetch, int fetchCount, int threadCount, int mutateThreadCount, int facetThreadCount, DeepPagingCache deepPagingCache, MemoryAllocationWatcher memoryAllocationWatcher, QueryStatusManager statusManager) { _statusManager = statusManager; _memoryAllocationWatcher = memoryAllocationWatcher; _deepPagingCache = deepPagingCache; _indexServer = indexServer; _clusterStatus = clusterStatus; _filterCache = filterCache; MetricName metricName1 = new MetricName(ORG_APACHE_BLUR, BLUR, "External Queries/s"); MetricName metricName2 = new MetricName(ORG_APACHE_BLUR, BLUR, "Internal Queries/s"); MetricName metricName3 = new MetricName(ORG_APACHE_BLUR, BLUR, "Fetch Timer"); _queriesExternalMeter = Metrics.newMeter(metricName1, "External Queries/s", TimeUnit.SECONDS); _queriesInternalMeter = Metrics.newMeter(metricName2, "Internal Queries/s", TimeUnit.SECONDS); _fetchTimer = Metrics.newTimer(metricName3, TimeUnit.MICROSECONDS, TimeUnit.SECONDS); if (threadCount == 0) { throw new RuntimeException("Thread Count cannot be 0."); } _threadCount = threadCount; if (mutateThreadCount == 0) { throw new RuntimeException("Mutate Thread Count cannot be 0."); } _mutateThreadCount = mutateThreadCount; _fetchCount = fetchCount; _maxHeapPerRowFetch = maxHeapPerRowFetch; _executor = Executors.newThreadPool("index-manager", _threadCount); _mutateExecutor = Executors.newThreadPool("index-manager-mutate", _mutateThreadCount); if (facetThreadCount < 1) { _facetExecutor = null; } else { _facetExecutor = Executors.newThreadPool(new SynchronousQueue<Runnable>(), "facet-execution", facetThreadCount); } LOG.info("Init Complete"); }
Example #18
Source File: ThreadPoolUtils.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 固定大小线程池,无队列 * * @param corePoolSize 初始化线程池 * @return the thread pool executor */ public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize) { return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); }
Example #19
Source File: RuntimeWorkerThreadFactory.java From es6draft with MIT License | 5 votes |
/** * Returns a new {@link ThreadPoolExecutor} to create runtime worker threads. * * @return a new {@link ThreadPoolExecutor} for runtime worker threads */ static ThreadPoolExecutor createWorkerThreadPoolExecutor() { String name = "runtimeworker-" + runtimeWorkerCount.incrementAndGet(); ThreadPoolExecutor executor = new ThreadPoolExecutor(WORKER_THREAD_CORE_SIZE, WORKER_THREAD_POOL_SIZE, WORKER_THREAD_POOL_TTL, TimeUnit.SECONDS, new SynchronousQueue<>(), new RuntimeWorkerThreadFactory(name)); executor.allowCoreThreadTimeOut(true); return executor; }
Example #20
Source File: TcpServer.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
private static PooledExecutorWithDMStats createExecutor(PoolStatHelper poolHelper, final ThreadGroup threadGroup) { ThreadFactory factory = new ThreadFactory() { private final AtomicInteger threadNum = new AtomicInteger(); public Thread newThread(Runnable r) { Thread thread = new Thread(threadGroup, r, "locator request thread[" + threadNum.incrementAndGet() + "]"); thread.setDaemon(true); return thread; } }; return new PooledExecutorWithDMStats(new SynchronousQueue(), MAX_POOL_SIZE, poolHelper, factory, POOL_IDLE_TIMEOUT, new ThreadPoolExecutor.CallerRunsPolicy()); }
Example #21
Source File: FixedThreadPool.java From anima with GNU General Public License v3.0 | 5 votes |
public Executor getExecutor(AppConf conf) { String name = conf.get(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME); int threads = conf.getInt(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); int queues = conf.getInt(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES); return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, queues == 0 ? new SynchronousQueue<Runnable>() : (queues < 0 ? new LinkedBlockingQueue<Runnable>() : new LinkedBlockingQueue<Runnable>(queues)), new NamedThreadFactory(name, true), new AbortPolicyWithReport(name)); }
Example #22
Source File: Db.java From arcusplatform with Apache License 2.0 | 5 votes |
Db(String name, @Nullable File dbFile, int numWorkers, long walModeCheckpointMs) { this.name = name; this.walModeCheckpointNs = TimeUnit.NANOSECONDS.convert(walModeCheckpointMs, TimeUnit.MILLISECONDS); this.dbFile = dbFile; this.numWorkers = numWorkers; this.queue = new SynchronousQueue<>(); this.checkpointer = (walModeCheckpointMs > 0) ? new DbCheckpointer() : null; }
Example #23
Source File: SynchronousQueueTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testAddAll_self(boolean fair) { SynchronousQueue q = new SynchronousQueue(fair); try { q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} }
Example #24
Source File: TestMessageReceiverImpl.java From datacollector with Apache License 2.0 | 5 votes |
@Test public void interruptReceive() throws Exception { SynchronousQueue<MessageReplyConsumerBundle> queue = new SynchronousQueue<>(); MessageReceiver messageReceiver = new MessageReceiverImpl(queue); PubsubMessage message = PubsubMessage.newBuilder().setMessageId("1234").build(); AckReplyConsumer consumer = mock(AckReplyConsumer.class); Thread t = new Thread(() -> messageReceiver.receiveMessage(message, consumer)); t.start(); t.interrupt(); ThreadUtil.sleep(50); verify(consumer, times(1)).nack(); }
Example #25
Source File: ThreadPoolExecutorsTest.java From riptide with MIT License | 5 votes |
@Test void emptyBoundedQueueEqualsNoQueue() { final ThreadPoolExecutor executor = ThreadPoolExecutors.builder() .boundedQueue(0) .fixedSize(1) .keepAlive(Duration.ofMinutes(1)) .build(); assertThat(executor.getCorePoolSize(), is(1)); assertThat(executor.getMaximumPoolSize(), is(1)); assertThat(executor.getQueue(), is(instanceOf(SynchronousQueue.class))); assertThat(executor.allowsCoreThreadTimeOut(), is(false)); }
Example #26
Source File: PooledExecutorWithDMStats.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
private static RejectedExecutionHandler initREH(BlockingQueue<Runnable> q) { if (q instanceof SynchronousQueue || q instanceof SynchronousQueueNoSpin) { return new CallerRunsPolicy(); //return new BlockHandler(); } else { // create a thread that takes from bufferQueue and puts into result return new BufferHandler(); } }
Example #27
Source File: LobstackWorkThread.java From jelectrum with MIT License | 5 votes |
public LobstackWorkThread(SynchronousQueue<WorkUnit> queue) { this.queue = queue; setName("LobstackWorkThread"); setDaemon(true); }
Example #28
Source File: ThreadPoolUtilsTest.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Test public void newFixedThreadPool1() throws Exception { BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>(); ThreadPoolExecutor executor = ThreadPoolUtils.newFixedThreadPool(10, queue); Assert.assertEquals(executor.getCorePoolSize(), 10); Assert.assertEquals(executor.getMaximumPoolSize(), 10); Assert.assertEquals(executor.getQueue(), queue); }
Example #29
Source File: SingletonValueJUnitTest.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
public void testMultithreadError() throws Exception { int count = 100; ExecutorService exec = Executors.newFixedThreadPool(count); final SynchronousQueue<Object> sync = new SynchronousQueue<Object>(true); final CountDownLatch waiting = new CountDownLatch(count - 1); final MockCallable call = new MockCallable(sync, waiting, true); final SingletonValue<MockCallable> cc = new SingletonValue<MockCallable>(call); final CyclicBarrier barrier = new CyclicBarrier(count + 1); Collection<Future<MockCallable>> results = new ArrayList<Future<MockCallable>>(); invoke(count, exec, cc, barrier, results); // wait for everyone barrier.await(); waiting.await(); // now release the originator sync.put(new Object()); for (Future<MockCallable> fu : results) { try { fu.get(); fail("Expected IOException"); } catch (Exception e) { } } exec.shutdownNow(); }
Example #30
Source File: Executor.java From PermissionAgent with Apache License 2.0 | 5 votes |
public Executor() { mHandler = new Handler(Looper.getMainLooper()); mExecutor = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); ; mExecutor.allowCoreThreadTimeOut(true); }