List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
From source file:Main.java
public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize) { return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); }
From source file:Main.java
@Deprecated public static ExecutorService newFixedThreadPool(int nThreads) { ThreadPoolExecutor exec = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(nThreads)); exec.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return exec;//from w ww . j a va2 s .c om }
From source file:Main.java
public static ThreadPoolExecutor createSingleThreadedPoolExecutor() { return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
From source file:Main.java
public static ThreadPoolExecutor createThreadPool() { ThreadPoolExecutor pool = new ThreadPoolExecutor(NUM_PROCESSORS, NUM_PROCESSORS, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); return pool;//from w w w. j a v a2s. com }
From source file:Main.java
public static ExecutorService newDynamicSingleThreadedExecutor() { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); executor.allowCoreThreadTimeOut(true); return executor; }
From source file:Main.java
public static ExecutorService getThreadPoolExecutor() { if (executor == null) { executor = new ThreadPoolExecutor(2, 5, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(3)); }/*w w w .j ava 2 s . c o m*/ return executor; }
From source file:Main.java
public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize, BlockingQueue<Runnable> queue) { return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, queue); }
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) { }// w w w . j a v a 2s.c om } 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 void runTest() throws Exception { ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); tp.setRejectedExecutionHandler(/* w ww .j a v a 2 s .co m*/ (Runnable r, ThreadPoolExecutor executor) -> System.out.println("Task rejected: " + r)); Semaphore oneTaskDone = new Semaphore(0); tp.execute(() -> { System.out.println("Sleeping"); try { Thread.sleep(300); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done sleeping"); oneTaskDone.release(); }); tp.execute(new Runnable() { @Override public void run() { System.out.println("Never happends"); } @Override public String toString() { return "Rejected Runnable"; } }); oneTaskDone.acquire(); tp.execute(() -> System.out.println("Running")); tp.shutdown(); tp.awaitTermination(100, TimeUnit.MILLISECONDS); System.out.println("Finished"); }
From source file:Main.java
public static ThreadPoolExecutor newLimitedThreadPool(int minNumberOfThreads, int maxNumberOfThreads, long defaultRemoveIdleThread, int bufferSize) { return new ThreadPoolExecutor(minNumberOfThreads, maxNumberOfThreads, defaultRemoveIdleThread, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(bufferSize)); }