public void testGet_concurrent() { assertTrue(ArbitraryInstances.get(BlockingDeque.class).isEmpty()); assertTrue(ArbitraryInstances.get(BlockingQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(DelayQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(SynchronousQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(PriorityBlockingQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(ConcurrentMap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ConcurrentNavigableMap.class).isEmpty()); ArbitraryInstances.get(Executor.class).execute(ArbitraryInstances.get(Runnable.class)); assertNotNull(ArbitraryInstances.get(ThreadFactory.class)); assertFreshInstanceReturned( BlockingQueue.class, BlockingDeque.class, PriorityBlockingQueue.class, DelayQueue.class, SynchronousQueue.class, ConcurrentMap.class, ConcurrentNavigableMap.class, AtomicReference.class, AtomicBoolean.class, AtomicInteger.class, AtomicLong.class, AtomicDouble.class); }
private <T> Collection<T> instantiateCollectionFromInterface(Class<? extends T> collectionType) { if (List.class.isAssignableFrom(collectionType)) { return new ArrayList<T>(); } else if (SortedSet.class.isAssignableFrom(collectionType)) { return new TreeSet<T>(); } else if (Set.class.isAssignableFrom(collectionType)) { return new LinkedHashSet<T>(); } else if (BlockingDeque.class.isAssignableFrom(collectionType)) { return new LinkedBlockingDeque<T>(); } else if (Deque.class.isAssignableFrom(collectionType)) { return new ArrayDeque<T>(); } else if (BlockingQueue.class.isAssignableFrom(collectionType)) { return new LinkedBlockingDeque<T>(); } else if (Queue.class.isAssignableFrom(collectionType)) { return new LinkedList<T>(); } return new ArrayList<T>(); }
/** * takeFirst() blocks interruptibly when empty */ public void testTakeFirstFromEmptyBlocksInterruptibly() { final BlockingDeque q = new LinkedBlockingDeque(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { threadStarted.countDown(); try { q.takeFirst(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(threadStarted); assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); }
/** * takeLast() blocks interruptibly when empty */ public void testTakeLastFromEmptyBlocksInterruptibly() { final BlockingDeque q = new LinkedBlockingDeque(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { threadStarted.countDown(); try { q.takeLast(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(threadStarted); assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); }
static void putAllCollections(Map<Class<?>, IntFunction<?>> map, Map<Class<?>, Function<?, ?>> unmodMap) { safePut(map, ArrayList.class, ArrayList::new); safePut(map, HashSet.class, LinkedHashSet::new); safePut(map, Properties.class, x -> new Properties()); safePut(map, Hashtable.class, Hashtable::new); safePut(map, Collection.class, ArrayList::new); safePut(map, Set.class, LinkedHashSet::new); safePut(map, List.class, ArrayList::new); safePut(map, SortedSet.class, x -> new TreeSet<>()); safePut(map, Queue.class, x -> new ConcurrentLinkedQueue<>()); safePut(map, Deque.class, x -> new ConcurrentLinkedDeque<>()); safePut(map, BlockingQueue.class, x -> new LinkedBlockingQueue<>()); safePut(map, BlockingDeque.class, x -> new LinkedBlockingDeque<>()); safePut(map, HashMap.class, LinkedHashMap::new); safePut(map, LinkedHashMap.class, LinkedHashMap::new); safePut(map, ConcurrentHashMap.class, ConcurrentHashMap::new); safePut(map, Map.class, LinkedHashMap::new); safePut(map, ConcurrentMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, ConcurrentNavigableMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, SortedMap.class, i -> new TreeMap<>()); }
/** * takeFirst() blocks interruptibly when empty */ public void testTakeFirstFromEmptyBlocksInterruptibly() { final BlockingDeque q = new LinkedBlockingDeque(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { threadStarted.countDown(); try { q.takeFirst(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(threadStarted); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); }
/** * takeLast() blocks interruptibly when empty */ public void testTakeLastFromEmptyBlocksInterruptibly() { final BlockingDeque q = new LinkedBlockingDeque(); final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { threadStarted.countDown(); try { q.takeLast(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); await(threadStarted); assertThreadStaysAlive(t); t.interrupt(); awaitTermination(t); }
private void finishScanningCycle() { for (Map.Entry<String, BlockingDeque<Integer>> entry : mScanHistory.entrySet()) { final BlockingDeque<Integer> values = entry.getValue(); if (values.size() < BUFFER_SIZE) { Log.d(TAG, entry.getKey() + ": Sorry, only got " + values.size() + " out of " + BUFFER_SIZE); } DescriptiveStatistics stats = BeaconFilter.statistics(values); synchronized (mScanResults) { for (ScanItem info : mScanResults) { if (info.getMacAddress().equalsIgnoreCase((entry.getKey()))) { info.setRssi((int) stats.getMean()); Log.d(TAG, entry.getKey() + ": Calculated mean of RSSI to " + info.getRssi()); break; } } } mScanHistory.get(entry.getKey()).clear(); } parcelIntent.putParcelableArrayListExtra(BluetoothScannerService.TAG_PARCEL, mScanResults); sendBroadcast(parcelIntent); }
static void putAllCollections(Map<Class<?>, IntFunction<?>> map) { safePut(map, ArrayList.class, ArrayList::new); safePut(map, HashSet.class, LinkedHashSet::new); safePut(map, Properties.class, x -> new Properties()); safePut(map, Hashtable.class, Hashtable::new); safePut(map, Collection.class, ArrayList::new); safePut(map, Set.class, LinkedHashSet::new); safePut(map, List.class, ArrayList::new); safePut(map, SortedSet.class, x -> new TreeSet<>()); safePut(map, Queue.class, x -> new ConcurrentLinkedQueue<>()); safePut(map, Deque.class, x -> new ConcurrentLinkedDeque<>()); safePut(map, BlockingQueue.class, x -> new LinkedBlockingQueue<>()); safePut(map, BlockingDeque.class, x -> new LinkedBlockingDeque<>()); safePut(map, HashMap.class, LinkedHashMap::new); safePut(map, LinkedHashMap.class, LinkedHashMap::new); safePut(map, ConcurrentHashMap.class, ConcurrentHashMap::new); safePut(map, Map.class, LinkedHashMap::new); safePut(map, ConcurrentMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, ConcurrentNavigableMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, SortedMap.class, i -> new TreeMap<>()); }
private static void startFuzzer(String targetHost, Integer targetPort, Integer fuzzerPort, String fuzzType) throws IOException { BlockingDeque<String> queue = new LinkedBlockingDeque<String>(); AgentEventReader reader = new AgentEventReader(queue, fuzzerPort); PayloadGenerator generator = new RandomBytesPayloadGenerator(); PayloadSender sender = null; if ("TCP".equals(fuzzType)) { sender = new TCPSocketPayloadSender(targetHost, targetPort); } else if ("HTTP".equals(fuzzType)) { // TODO implement payload generator for http } else if ("JMX".equals(fuzzType)) { // TODO implement payload generator for jmx } PayloadService service = new PayloadService(queue, generator, sender); System.out.println(format("Fuzzer has started on %s:%d", targetHost, targetPort)); ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(reader); executorService.execute(service); }
@SuppressWarnings("unchecked") private static BlockingDeque<String> proxy(Object inputDeque) throws NoSuchMethodException { if (inputDeque instanceof BlockingDeque) { return (BlockingDeque<String>) inputDeque; } final ClassLoader cl = Thread.currentThread().getContextClassLoader(); final Method put = inputDeque.getClass().getMethod("put", Object.class); final Method take = inputDeque.getClass().getMethod("take"); final Method isEmpty = inputDeque.getClass().getMethod("isEmpty"); return (BlockingDeque<String>)Proxy.newProxyInstance(cl, new Class[]{BlockingDeque.class}, (proxy, method, args) -> { switch (method.getName()) { case "put": return put.invoke(inputDeque, args); case "take": return take.invoke(inputDeque, args); case "isEmpty": return isEmpty.invoke(inputDeque, args); } throw new UnsupportedOperationException(method.toGenericString() + " is not supported"); }); }
@Override public void pack(BlockingDeque<PackItem> itemsDeque, File packsDir, String packId, Consumer<PackItem> newItemSuccess, Consumer<PackItem> newItemFail, Consumer<File> done, Runnable failed) { final File packFile = new File(packsDir, packId + ".zip"); try (final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(packFile))) { PackItem item = takeItem(itemsDeque); if (item == null) { failed.run(); return; } while (!item.getFileName().equals("end")) { final boolean added = PackSupport.saveToZip(item, zos); if (added) newItemSuccess.accept(item); else newItemFail.accept(item); item = takeItem(itemsDeque); if (item == null) { failed.run(); return; } } } catch (IOException e) { failed.run(); return; } done.accept(packFile); }
private <I extends PackItem> I takeItem(BlockingDeque<I> deque) { try { return deque.take(); } catch (InterruptedException e) { return null; } }
void pack(BlockingDeque<PackItem> itemsDeque, File packsDir, String packId, Consumer<PackItem> newItemSuccess, Consumer<PackItem> newItemFail, Consumer<File> done, Runnable failed);
@Override public void pack(BlockingDeque<PackItem> itemsDeque, File packsDir, String packId, Consumer<PackItem> newItemSuccess, Consumer<PackItem> newItemFail, Consumer<File> done, Runnable failed) { if (!packsDir.exists()) throw new IllegalArgumentException("packsDir does not exist"); final File packDir = new File(packsDir, packId); if (!packDir.mkdir()) throw new RuntimeException("unable to create pack directory"); PackItem item = takeItem(itemsDeque); if (item == null) { failed.run(); return; } while (!item.getFileName().equals("end")) { try { final File itemFile = new File(packDir, item.getFileName()); PackSupport.save(new FileOutputStream(itemFile), item.getUrl()); newItemSuccess.accept(item); item = takeItem(itemsDeque); if (item == null) { failed.run(); return; } } catch (IOException e) { newItemFail.accept(item); } } done.accept(packDir); }
@Override public Pack createPack(C config) { requireNonNull(config, "required non null config"); final String id = config.getUniqueId(); if (packs.containsKey(id)) return packs.get(id); final Pack pack = new Pack(id, config.numberOfItems()); packs.put(id, pack); final BlockingDeque<PackItem> deque = new LinkedBlockingDeque<>(); executorService.submit(() -> repository.getPackItems(config, deque)); pack.processing(); executorService.submit(() -> packer.pack(deque, packsDir, pack)); return pack; }
/** * takeFirst() throws InterruptedException immediately if interrupted * before waiting */ public void testTakeFirstFromEmptyAfterInterrupt() { final BlockingDeque q = new LinkedBlockingDeque(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { Thread.currentThread().interrupt(); try { q.takeFirst(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); awaitTermination(t); }
/** * takeLast() throws InterruptedException immediately if interrupted * before waiting */ public void testTakeLastFromEmptyAfterInterrupt() { final BlockingDeque q = new LinkedBlockingDeque(); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { Thread.currentThread().interrupt(); try { q.takeLast(); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); }}); awaitTermination(t); }
@Override public void parse() throws IOException { BlockingDeque<ConfigElement> stack = new LinkedBlockingDeque<ConfigElement>(); ConfigElement root = new ConfigElement("root"); this.config = root; stack.addLast(root); ConfigElement current = root; StringBuilder sb = new StringBuilder(); readConfigurationText(sb); List<String> tokens = tokenize(sb); int i = 1; for (String token : tokens) { if (log.isTraceEnabled()) { log.trace("token " + i++ + "/" + tokens.size() + ":" + token); } JunosElementType thisType = getType(token); if (thisType == JunosElementType.LAYER_ENTER) { String id = token.replaceAll("[ ]+\\{", ""); ConfigElement e = new ConfigElement(id); current.addElement(e); stack.addLast(e); current = e; } else if (thisType.equals(JunosElementType.LAYER_LEAVE)) { stack.removeLast(); current = stack.peekLast(); } else if (thisType.equals(JunosElementType.ATTRIBUTE)) { token = token.substring(0, token.length() - 1); current.addAttribute(token); } } }
public PayloadService(BlockingDeque<String> queue, PayloadGenerator generator, PayloadSender sender) { this.queue = checkNotNull(queue); this.generator = checkNotNull(generator); this.sender = checkNotNull(sender); }
public FileHasher(Context context, AtomicBoolean scanInProgress, HashProgress hashProgress, BlockingDeque<Path> filesToHashQueue, String rootDir) throws NoSuchAlgorithmException { this.context = context; this.scanInProgress = scanInProgress; this.hashProgress = hashProgress; this.filesToHashQueue = filesToHashQueue; this.rootDir = rootDir; this.fileStates = new ArrayList<>(); this.frontHasher = new FrontHasher(context); }
private void scanFileTree(BlockingDeque<Path> filesToHashQueue, Path directory, FimIgnore parentFimIgnore) throws NoSuchAlgorithmException { try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) { FimIgnore fimIgnore = fimIgnoreManager.loadLocalIgnore(directory, parentFimIgnore); for (Path file : stream) { if (!hashProgress.isHashStarted() && filesToHashQueue.size() > FILES_QUEUE_CAPACITY / 2) { startFileHashers(); } BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); String fileName = file.getFileName().toString(); if (fimIgnoreManager.isIgnored(fileName, attributes, fimIgnore)) { fimIgnoreManager.ignoreThisFiles(file, attributes); } else { if (attributes.isRegularFile()) { if (FilePattern.matchPatterns(fileName, context.getIncludePatterns(), true) && !FilePattern.matchPatterns(fileName, context.getExcludePatterns(), false)) { enqueueFile(filesToHashQueue, file); } } else if (attributes.isDirectory()) { scanFileTree(filesToHashQueue, file, fimIgnore); } } } } catch (IOException ex) { Logger.newLine(); Logger.error("Skipping - Error scanning directory '" + directory + "'", ex, context.isDisplayStackTrace()); } }
private void enqueueFile(BlockingDeque<Path> filesToHashQueue, Path file) { try { filesToHashQueue.offer(file, 120, TimeUnit.MINUTES); } catch (InterruptedException ex) { Logger.error("Exception while enqueuing file '" + file + "'", ex, context.isDisplayStackTrace()); } }
public JobExecutor() { BlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>(); ThreadFactory threadFactory = new JobThreadFactory(); this.threadPoolExecutor = new ThreadPoolExecutor( INITIAL_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, workQueue, threadFactory ); }
@SuppressWarnings("unchecked") @Override protected void setUp() throws Exception { super.setUp(); final BlockingDeque<String> deque = createProxyInstance(BlockingDeque.class); forward = new ForwardingBlockingDeque<String>() { @Override protected BlockingDeque<String> delegate() { return deque; } }; }
public static void main(String[] args) throws InterruptedException { BlockingDeque<String> bq = new LinkedBlockingDeque<>(); String str = "test"; bq.put(str); str = "yesorno"; bq.put(str); System.out.println(bq.size()); System.out.println(bq.take()); System.out.println(bq.take()); }
public void driver(){ BlockingDeque urls = new LinkedBlockingDeque(10); ExecutorService service = Executors.newFixedThreadPool(10); List<Crawler> runners = new ArrayList<Crawler>(); for(int i=0; i<10; i++){ Crawler c = new Crawler(urls, runners); runners.add(c); service.submit(c); } }
AggregateContainer(final HeartBeatTriggerManager heartBeatTriggerManager, final KryoUtils kryoUtils, final BlockingDeque<byte[]> workerReportsQueue, final TaskletAggregationRequest taskletAggregationRequest) { this.heartBeatTriggerManager = heartBeatTriggerManager; this.kryoUtils = kryoUtils; this.workerReportsQueue = workerReportsQueue; this.taskletAggregationRequest = taskletAggregationRequest; }
public GwtcJobMonitorImpl(BlockingDeque<String> caller, BlockingDeque<String> compiler) { // We were instantiated directly, so assume we are running in a foreign classloader, // and that our parameters are a pair of LinkedBlockingDeque's that we can use to communicate with. readAsCaller = compiler::take; readAsCompiler = caller::take; writeAsCaller = caller::put; writeAsCompiler = compiler::put; hasCallerOutput = ()->!caller.isEmpty(); hasCompilerOutput = ()->!compiler.isEmpty(); }
@NotNull @Override protected AtomicBoolean startMessageSender() { final String ourContainerString = ourContainerString(OurProviderIdentifier, OurRepositoryIdentifier, instanceId); final Target target = new Target(newKnownGoodAddressString(queueName), unsettled_state, session_end, LinkExpiryZeroTimeout, true, new NodeProperties(DeleteOnClose.DeleteOnClose), EmptyTargetCapabilities); final BlockingQueue<MessageToEnqueue> messagesToEnqueue = sendingStuffEventMessageUser.messagesToEnqueue; return startMessageSendingOnlyAmqpConnectionThread(AttemptToNegotiateAmqpTlsSecurityLayer, hostName, virtualHostName, ourContainerString, user, base64urlEncodedPassword, (BlockingDeque<MessageToEnqueue>) messagesToEnqueue, target, persistedMessagesPath, true); }
@Override @NotNull protected AtomicBoolean startMessageSender() { final String ourContainerString = ourContainerString(OurProviderIdentifier, OurRepositoryIdentifier, instanceId); final Target target = new Target(newKnownGoodAddressString(queueName), unsettled_state, session_end, LinkExpiryZeroTimeout, true, new NodeProperties(DeleteOnClose.DeleteOnClose), EmptyTargetCapabilities); final BlockingQueue<MessageToEnqueue> messagesToEnqueue = sendingStuffEventMessageUser.messagesToEnqueue; return startMessageSendingOnlyAmqpConnectionThread(AttemptToNegotiateAmqpTlsSecurityLayer, hostName, virtualHostName, ourContainerString, user, base64urlEncodedPassword, (BlockingDeque<MessageToEnqueue>) messagesToEnqueue, target, persistedMessagesPath, true); }