List of usage examples for java.util.concurrent TimeUnit SECONDS
TimeUnit SECONDS
To view the source code for java.util.concurrent TimeUnit SECONDS.
Click Source Link
From source file:Main.java
/** * Gets a long value i.e the time when the call was made and then returns a * appropriate string/*from w w w . j a va 2 s.c o m*/ * * @param callDate a long value which represents the time when the call was made * @return a formatted string to be displayed. */ public static String getTheTimePassedSinceCall(long callDate) { long difference = System.currentTimeMillis() - callDate; long diffInSeconds = getDateDiff(difference, TimeUnit.SECONDS); long diffInMin = getDateDiff(difference, TimeUnit.MINUTES); long diffInHours = getDateDiff(difference, TimeUnit.HOURS); return getDifferenceString(diffInSeconds, diffInMin, diffInHours, callDate); }
From source file:Main.java
/** * @param exec/* www . ja v a 2s .c om*/ * @param checkInterval * @param shutdown */ public static void checkBlockPoolCompleted(ThreadPoolExecutor exec, int checkInterval, boolean shutdown) { while (true) { int activite = exec.getActiveCount(); waitFor(checkInterval); if (activite == 0) { break; } } if (shutdown) { exec.shutdown(); try { exec.awaitTermination(60, TimeUnit.SECONDS); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }
From source file:Main.java
public static void addExecutorShutdownHook(final ExecutorService exec) { addExecutorShutdownHook(exec, 60, TimeUnit.SECONDS); }
From source file:Main.java
/** * @param threadToCpuRatio - for example, assuming you have 2 CPUs and setting a threadToCpuRation to 3, the result will be a pool with 6 working threads. * @return an {@link ExecutorService} with defined amount of worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy} *///from ww w . j a v a 2 s .c om public static ExecutorService getQueuedThreadPool(double threadToCpuRatio, int queueCapacity) { int workingThreads = Double.valueOf(NUM_CPU * threadToCpuRatio).intValue(); return new ThreadPoolExecutor(workingThreads, workingThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(queueCapacity), new ThreadPoolExecutor.CallerRunsPolicy()); }
From source file:Main.java
/** * close the thread pool safely.// w w w . j av a2s . c o m * @param pool */ public static void safeClose(ExecutorService pool) { if (pool != null) { pool.shutdown(); try { if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); } } catch (InterruptedException ex) { //ignore the ex } } }
From source file:Main.java
/** * Create a cached thread pool whose max number of threads is `maxThreadNumber`. Thread names * are formatted as prefix-ID, where ID is a unique, sequentially assigned integer. *///from w w w . jav a2 s.c o m public static ThreadPoolExecutor newDaemonCachedThreadPool(String prefix, int maxThreadNumber) { ThreadFactory threadFactory = namedThreadFactory(prefix); return new ThreadPoolExecutor(0, maxThreadNumber, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
From source file:Main.java
public static int parseDuration(String dur) { if (TextUtils.isEmpty(dur)) return 0; String[] strings = dur.split(":"); if (strings.length != 3) return 0; try {//w w w. j a v a 2 s . co m int sec = 0; if (!TextUtils.isEmpty(strings[0])) { sec += TimeUnit.SECONDS.convert(Integer.decode(strings[0]), TimeUnit.HOURS); } sec += TimeUnit.SECONDS.convert(Integer.decode(strings[1]), TimeUnit.MINUTES); sec += TimeUnit.SECONDS.convert(Integer.decode(strings[2].substring(0, 2)), TimeUnit.SECONDS); return sec; } catch (NumberFormatException e) { return 0; } }
From source file:Main.java
/** * @param threadToCpuRatio - for example, assuming you have 2 CPUs and setting a threadToCpuRation to 3, the result will be a pool with 6 working threads. * @return an {@link ExecutorService} with defined amount of worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy} *//*from w w w . j ava2 s . com*/ public static ExecutorService getTightThreadPool(double threadToCpuRatio) { int workingThreads = Double.valueOf(NUM_CPU * threadToCpuRatio).intValue(); return new ThreadPoolExecutor(workingThreads, workingThreads, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(true), new ThreadPoolExecutor.CallerRunsPolicy()); }
From source file:Main.java
public static void shutdownAndAwaitTermination(ExecutorService pool) { if (pool != null) { pool.shutdown(); // Disable new tasks from being submitted try {/*from w w w . ja va 2s .c o m*/ // Wait a while for existing tasks to terminate if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { pool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { throw new InterruptedException("Pool did not terminate"); } } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted pool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } } }
From source file:Main.java
/** * Attempts to gracefully shutdown the given {@link ExecutorService}. * * @param threadPool the {@code ExecutorService} to shutdown *///w ww . j ava 2 s . co m public static void shutdownAndAwaitTermination(ExecutorService threadPool) { if (threadPool == null) return; threadPool.shutdown(); // Disable new tasks from being submitted try { // Wait a while for existing tasks to terminate if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) { threadPool.shutdownNow(); // Cancel currently executing tasks // Wait a while for tasks to respond to being cancelled if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) System.err.println("Pool did not terminate"); } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted threadPool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } }