List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue
public LinkedBlockingQueue()
From source file:Main.java
public static void blockUntilConnected(final SocketChannel channel, long timeout) throws IOException { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, timeout, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() { public Boolean call() { while (!channel.isConnected()) { try { Thread.sleep(300); } catch (InterruptedException e) { }//from w w w . j a v a 2 s. c o m } return true; } }); executor.execute(future); try { future.get(timeout, TimeUnit.MILLISECONDS); } catch (Exception e) { channel.close(); throw new IOException(e); } }
From source file:Main.java
public static ThreadPoolExecutor createExecutor(final String name, int count, int keepAlive, final boolean isDaemon) { ThreadPoolExecutor exe = new ThreadPoolExecutor(count, count, keepAlive, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { private int threadNum = 1; public Thread newThread(Runnable r) { Thread t = new Thread(r, name + (threadNum++)); t.setDaemon(isDaemon); return t; }// w w w .j a va 2s . c o m }); // if (keepAlive > 0) { // // FIXME JDK 1.7 ? // if (SystemUtils.IS_JAVA_1_5 == false) { // try { // exe.allowCoreThreadTimeOut(true); // } catch(Throwable t) { } // } // } return exe; }
From source file:Main.java
public static ExecutorService newFixedThreadPool(String name, int numThreads, int maxPoolSize, int keepAliveTimeInSeconds) { LinkedBlockingQueue<Runnable> lbq = new LinkedBlockingQueue<Runnable>(); ThreadFactory tf = newNamedThreadFactory(name); ThreadPoolExecutor tpe = new ThreadPoolExecutor(numThreads, maxPoolSize, keepAliveTimeInSeconds, TimeUnit.SECONDS, lbq, tf); return Executors.newFixedThreadPool(numThreads, tpe.getThreadFactory()); }
From source file:Main.java
/** * Similar to {@link #newCoalescingThreadPool(String)}, but always runs the * last runnable put into the pool. If runnable a is executed, and then * runnables b and c are executed while a is still running, runnable b will * be dropped and c will executed./*from www . ja v a 2 s . c o m*/ */ public static ExecutorService newLastRequestThreadPool(String name) { return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>() { @Override public boolean offer(Runnable e) { clear(); return super.offer(e); } }, newNamedThreadFactory(name)); }
From source file:au.org.ala.layers.util.BatchConsumer.java
synchronized public static void start(LayerIntersectDAO layerIntersectDao, String batchDir) { if (threads.size() == 0) { waitingBatchDirs = new LinkedBlockingQueue<String>(); int size = Integer.parseInt( (new UserProperties()).getProperties().getProperty("batch_sampling_parallel_requests", "1")); for (int i = 0; i < size; i++) { Thread t = new BatchConsumerThread(waitingBatchDirs, layerIntersectDao, batchDir); t.start();/* ww w. j av a2 s . c o m*/ threads.add(t); } //get jobs that may have been interrupted but a shutdown File f = new File(batchDir); File[] files = f.listFiles(); java.util.Arrays.sort(files); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory() && !(new File(files[i].getPath() + File.separator + "error.txt")).exists() && !(new File(files[i].getPath() + File.separator + "finished.txt")).exists()) { System.out.println("found incomplete batch_sampling: " + files[i].getPath()); try { addBatch(files[i].getPath() + File.separator); } catch (InterruptedException e) { } } } } }
From source file:Main.java
/** * Creates a single thread executor which ignores all executions that occur * while it is busy executing a Runnable. This is useful for tasks that may * be requested multiple times from multiple sources, but which only need to * take place once./*from w w w . jav a 2 s . co m*/ * * @param name the name for threads created within this pool */ @SuppressWarnings("serial") public static ExecutorService newCoalescingThreadPool(String name) { return new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), newNamedThreadFactory(name)) { private boolean executing = false; @Override public void execute(final Runnable command) { synchronized (this) { if (executing) return; executing = true; } super.execute(new Runnable() { @Override public void run() { try { command.run(); } finally { executing = false; } } }); } }; }
From source file:au.org.ala.spatial.util.BatchConsumer.java
synchronized public static void start(LayerIntersectDAO layerIntersectDao, String batchDir) { if (threads.size() == 0) { waitingBatchDirs = new LinkedBlockingQueue<String>(); int size = Integer.parseInt( (new UserProperties()).getProperties().getProperty("batch_sampling_parallel_requests", "1")); for (int i = 0; i < size; i++) { Thread t = new BatchConsumerThread(waitingBatchDirs, layerIntersectDao, batchDir); t.start();/*from w w w. jav a 2s.c o m*/ threads.add(t); } //get jobs that may have been interrupted but a shutdown File f = new File(batchDir); File[] files = f.listFiles(); Arrays.sort(files); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory() && !(new File(files[i].getPath() + File.separator + "error.txt")).exists() && !(new File(files[i].getPath() + File.separator + "finished.txt")).exists()) { System.out.println("found incomplete batch_sampling: " + files[i].getPath()); try { addBatch(files[i].getPath() + File.separator); } catch (InterruptedException e) { } } } } }
From source file:Main.java
/** * Given any of the known collection types, this method will return an instance of the collection. * @param collectionType the type of the collection * @return the collection instance//from ww w . ja va 2 s . c o m */ public static Collection<?> getCollection(Class<?> collectionType) { if (HashSet.class.equals(collectionType)) { return new HashSet<Object>(); } else if (TreeSet.class.equals(collectionType)) { return new TreeSet<Object>(); } else if (CopyOnWriteArraySet.class.equals(collectionType)) { return new CopyOnWriteArraySet<Object>(); } else if (LinkedHashSet.class.equals(collectionType)) { return new LinkedHashSet<Object>(); } else if (ArrayList.class.equals(collectionType)) { return new ArrayList<Object>(); } else if (LinkedList.class.equals(collectionType)) { return new LinkedList<Object>(); } else if (Vector.class.equals(collectionType)) { return new Vector<Object>(); } else if (Stack.class.equals(collectionType)) { return new Stack<Object>(); } else if (PriorityQueue.class.equals(collectionType)) { return new PriorityQueue<Object>(); } else if (PriorityBlockingQueue.class.equals(collectionType)) { return new PriorityBlockingQueue<Object>(); } else if (ArrayDeque.class.equals(collectionType)) { return new ArrayDeque<Object>(); } else if (ConcurrentLinkedQueue.class.equals(collectionType)) { return new ConcurrentLinkedQueue<Object>(); } else if (LinkedBlockingQueue.class.equals(collectionType)) { return new LinkedBlockingQueue<Object>(); } else if (LinkedBlockingDeque.class.equals(collectionType)) { return new LinkedBlockingDeque<Object>(); } else if (List.class.equals(collectionType)) { return new LinkedList<Object>(); } else if (Set.class.equals(collectionType)) { return new HashSet<Object>(); } else if (Queue.class.equals(collectionType)) { return new PriorityQueue<Object>(); } else if (Deque.class.equals(collectionType)) { return new ArrayDeque<Object>(); } else if (Collection.class.equals(collectionType)) { return new LinkedList<Object>(); } throw new IllegalArgumentException("Unsupported collection type: " + collectionType); }
From source file:MainClass.java
public SingleThreadAccess() { tpe = new ThreadPoolExecutor(1, 1, 50000L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); }
From source file:com.cloudhopper.commons.io.demo.FileServerMain.java
public static void loadFilesFromDir(String dir, int threads) { ThreadPoolExecutor ex = new ThreadPoolExecutor(threads, threads, 5000l, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ex.prestartAllCoreThreads();// w w w.j av a 2 s .c om IdGenerator idGen = new UUIDIdGenerator(); final FileStore store = new SimpleNIOFileStore(idGen, "/tmp/fileStore/"); final long start = System.currentTimeMillis(); int count = 0; Iterator<File> it = FileUtils.iterateFiles(new File(dir), null, true); while (it.hasNext()) { final File f = it.next(); final int num = count++; Runnable job = new Runnable() { @Override public void run() { try { RandomAccessFile randomAccessFile = new RandomAccessFile(f, "r"); FileChannel fileChannel = randomAccessFile.getChannel(); Id id = store.write(fileChannel); System.out.println("(" + num + ") Stored " + f.getPath() + " as " + id.getName() + " after " + (System.currentTimeMillis() - start) + "ms"); } catch (Exception e) { logger.error("", e); } } }; ex.execute(job); } }