List of usage examples for java.util.concurrent TimeUnit MILLISECONDS
TimeUnit MILLISECONDS
To view the source code for java.util.concurrent TimeUnit MILLISECONDS.
Click Source Link
From source file:Main.java
public static <T> T getFutureResult(Callable<T> task, long timeout) throws TimeoutException { try {//from ww w.j a v a 2 s.c o m ExecutorService executor = Executors.newFixedThreadPool(1); Future<T> future = executor.submit(task); T result = null; if (timeout <= 0) { System.out.println("get wait forever"); result = future.get(); } else { System.out.println("get wait " + timeout); result = future.get(timeout, TimeUnit.MILLISECONDS); } return result; } catch (java.util.concurrent.TimeoutException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static long getTime() { return TimeUnit.MILLISECONDS.toSeconds(new Date().getTime()); }
From source file:Main.java
/** * Return a prettified version of the given elapsed time * @return//from w ww .j a va2 s. c o m */ static String formatElapsedTime(long elapsedTimeMs) { long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTimeMs) % 60; long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTimeMs) % 60; long hours = TimeUnit.MILLISECONDS.toHours(elapsedTimeMs); StringBuilder time = new StringBuilder(); if (hours > 0) { time.append(hours); time.append("h "); } if (minutes > 0) { time.append(minutes); time.append("m "); } time.append(seconds); time.append("s"); return time.toString(); }
From source file:Main.java
private static int convertDelta(final long millis, TimeUnit to) { return (int) to.convert(System.currentTimeMillis() - millis, TimeUnit.MILLISECONDS); }
From source file:Main.java
public static String milliToMinutes(String milliseconds) { Long millis = Long.parseLong(milliseconds); milliseconds = String.format("%dmin : %dsec", TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); return milliseconds; }
From source file:Main.java
public static String getDaysBetween(String date1, String date2) { // input is expected to be exactly like; 2011-01-05 // date2 must be before date1 String result = ""; try {/* w ww . j a va 2 s. co m*/ Date dateOne = DateUtils.parseDate(date1, new String[] { "yyyy-MM-dd" }); Calendar cal1 = Calendar.getInstance(); cal1.setTime(dateOne); Date dateTwo = DateUtils.parseDate(date2, new String[] { "yyyy-MM-dd" }); Calendar cal2 = Calendar.getInstance(); cal2.setTime(dateTwo); long diff = dateOne.getTime() - dateTwo.getTime(); Log.d(TAG, "days in between:" + (TimeUnit.MILLISECONDS.toSeconds(diff) / 60 / 60 / 24)); } catch (Exception ex) { Log.w(TAG, ex.toString()); } return result; }
From source file:Main.java
public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize, BlockingQueue<Runnable> queue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, queue, threadFactory, handler);//from ww w .j av a 2 s . c o m }
From source file:Main.java
public static boolean stop(ExecutorService executorService, int waitBeforeTerminateSecs, Logger logger) /* */ {// w ww .j av a 2s.c o m /* 53 */int waitMillis = Math.max(1000, 1000 * waitBeforeTerminateSecs); /* */ /* */ /* 56 */executorService.shutdown(); /* */ /* */ /* 59 */boolean stopped = false; /* 60 */while ((waitMillis > 0) && (!stopped)) { /* 61 */long startMillis = System.currentTimeMillis(); /* */try { /* 63 */logger.debug("Waiting for thread pool to stop"); /* 64 */stopped = executorService.awaitTermination(waitMillis, TimeUnit.MILLISECONDS); /* */} catch (InterruptedException e) { /* 66 */logger.debug("Thread was interrupted while it was waiting for thread pool to stop", e); /* 67 */Thread.currentThread().interrupt(); /* 68 */break; /* */} /* 70 */waitMillis = (int) (waitMillis - (System.currentTimeMillis() - startMillis)); /* */} /* */ /* 73 */if (!executorService.isTerminated()) { /* 74 */logger.warn("Thread pool will be forcibly stopped now if it has not already stopped"); /* 75 */executorService.shutdownNow(); /* */try { /* 77 */stopped = executorService.awaitTermination(waitBeforeTerminateSecs, TimeUnit.SECONDS); /* */} /* */catch (InterruptedException e) { } /* */ /* 81 */if (!executorService.isTerminated()) { /* 82 */logger.warn("Could not shutdown thread pool in [{}] seconds", Integer.valueOf(waitBeforeTerminateSecs)); /* */} /* */} /* */ /* 86 */return stopped; /* */}
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 .jav a 2s . 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 ava2s. 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; }