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 long getUpdateInterval(@NonNull final String intervalFromPreference) { final int parsedValue = Integer.valueOf(intervalFromPreference); return TimeUnit.MILLISECONDS.convert(parsedValue, TimeUnit.HOURS); }
From source file:Main.java
public static <T> T executeWithTimeout(Callable<T> task, int milliseconds) { ExecutorService executor = Executors.newCachedThreadPool(); Future<T> future = executor.submit(task); T result;// ww w . j av a 2 s . c o m try { result = future.get(milliseconds, TimeUnit.MILLISECONDS); } catch (Exception e) { //handle timeout and other exceptions e.printStackTrace(); result = null; } finally { future.cancel(true); } return result; }
From source file:io.anserini.search.SearchWebCollection.java
public static void main(String[] args) throws Exception { SearchArgs searchArgs = new SearchArgs(); CmdLineParser parser = new CmdLineParser(searchArgs, ParserProperties.defaults().withUsageWidth(90)); try {//from ww w. java 2s .c om parser.parseArgument(args); } catch (CmdLineException e) { System.err.println(e.getMessage()); parser.printUsage(System.err); System.err.println("Example: SearchWebCollection" + parser.printExample(OptionHandlerFilter.REQUIRED)); return; } LOG.info("Reading index at " + searchArgs.index); Directory dir; if (searchArgs.inmem) { LOG.info("Using MMapDirectory with preload"); dir = new MMapDirectory(Paths.get(searchArgs.index)); ((MMapDirectory) dir).setPreload(true); } else { LOG.info("Using default FSDirectory"); dir = FSDirectory.open(Paths.get(searchArgs.index)); } Similarity similarity = null; if (searchArgs.ql) { LOG.info("Using QL scoring model"); similarity = new LMDirichletSimilarity(searchArgs.mu); } else if (searchArgs.bm25) { LOG.info("Using BM25 scoring model"); similarity = new BM25Similarity(searchArgs.k1, searchArgs.b); } else { LOG.error("Error: Must specify scoring model!"); System.exit(-1); } RerankerCascade cascade = new RerankerCascade(); boolean useQueryParser = false; if (searchArgs.rm3) { cascade.add(new Rm3Reranker(new EnglishAnalyzer(), FIELD_BODY, "src/main/resources/io/anserini/rerank/rm3/rm3-stoplist.gov2.txt")); useQueryParser = true; } else { cascade.add(new IdentityReranker()); } FeatureExtractors extractors = null; if (searchArgs.extractors != null) { extractors = FeatureExtractors.loadExtractor(searchArgs.extractors); } if (searchArgs.dumpFeatures) { PrintStream out = new PrintStream(searchArgs.featureFile); Qrels qrels = new Qrels(searchArgs.qrels); cascade.add(new WebCollectionLtrDataGenerator(out, qrels, extractors)); } Path topicsFile = Paths.get(searchArgs.topics); if (!Files.exists(topicsFile) || !Files.isRegularFile(topicsFile) || !Files.isReadable(topicsFile)) { throw new IllegalArgumentException( "Topics file : " + topicsFile + " does not exist or is not a (readable) file."); } TopicReader tr = (TopicReader) Class .forName("io.anserini.search.query." + searchArgs.topicReader + "TopicReader") .getConstructor(Path.class).newInstance(topicsFile); SortedMap<Integer, String> topics = tr.read(); final long start = System.nanoTime(); SearchWebCollection searcher = new SearchWebCollection(searchArgs.index); searcher.search(topics, searchArgs.output, similarity, searchArgs.hits, cascade, useQueryParser, searchArgs.keepstop); searcher.close(); final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS); LOG.info("Total " + topics.size() + " topics searched in " + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss")); }
From source file:Main.java
public static long timeInHours(long time) { return TimeUnit.MILLISECONDS.toHours(time); }
From source file:Main.java
public static boolean gracefulShutdown(ExecutorService threadPool, int shutdownTimeoutMills) { if (threadPool == null) { return true; }/* w w w . j av a 2 s .c om*/ return MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeoutMills, TimeUnit.MILLISECONDS); }
From source file:Main.java
public static int daysBetween(Date date1, Date date2) { long timespan = date2.getTime() - date1.getTime(); return (int) TimeUnit.DAYS.convert(timespan, TimeUnit.MILLISECONDS); }
From source file:Main.java
public static <T> T take(BlockingQueue<T> videoDrain, int ms) { try {//from w ww . j a v a 2s . c o m return videoDrain.poll(ms, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { return null; } }
From source file:Main.java
public static ThreadPoolExecutor newSingleThreadExecutor(ThreadFactory threadFactory) { return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);//w w w . j a v a2 s .c o m }
From source file:Main.java
public static String translatePastTimeIntoHumanReadableFormat(long pastTime) { long time = System.currentTimeMillis() - pastTime; if (pastTime == 0 || time < 0) { return "Just Now"; } else {//w w w.ja va 2 s . c o m long seconds = TimeUnit.MILLISECONDS.toSeconds(time); long minutes = TimeUnit.MILLISECONDS.toMinutes(time); long hours = TimeUnit.MILLISECONDS.toHours(time); long days = TimeUnit.MILLISECONDS.toDays(time); long years = (int) days / 365; if (years > 0) { if (years == 1) { return "1 year ago"; } else { return String.format("%d years ago", years); } } else if (days > 0) { if (days == 1) { return "Yesterday"; } else { return String.format("%d days ago", days); } } else if (hours > 0) { if (hours == 1) { return "About an hour ago"; } else { return String.format("%d hours ago", hours); } } else if (minutes > 0) { if (minutes == 1) { return "About one minute ago"; } else { return String.format("%d minutes ago", minutes); } } else { return "Just Now"; } } }
From source file:Main.java
public static long getTimeDiff(long start, long end) { long diffInMillis = start - end; long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillis); return diffInDays; }