List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:Main.java
public static void init(Context context) { mContext = context; mUiThread = Thread.currentThread(); mExecutor = Executors.newFixedThreadPool(5); }
From source file:edu.msu.cme.rdp.kmer.KmerFilter.java
public static void main(String[] args) throws Exception { final KmerTrie kmerTrie; final SeqReader queryReader; final SequenceType querySeqType; final File queryFile; final KmerStartsWriter out; final boolean translQuery; final int wordSize; final int translTable; final boolean alignedSeqs; final List<String> refLabels = new ArrayList(); final int maxThreads; try {//from ww w .j a v a 2 s .c o m CommandLine cmdLine = new PosixParser().parse(options, args); args = cmdLine.getArgs(); if (args.length < 3) { throw new Exception("Unexpected number of arguments"); } if (cmdLine.hasOption("out")) { out = new KmerStartsWriter(cmdLine.getOptionValue("out")); } else { out = new KmerStartsWriter(System.out); } if (cmdLine.hasOption("aligned")) { alignedSeqs = true; } else { alignedSeqs = false; } if (cmdLine.hasOption("transl-table")) { translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table")); } else { translTable = 11; } if (cmdLine.hasOption("threads")) { maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads")); } else { maxThreads = Runtime.getRuntime().availableProcessors(); } queryFile = new File(args[1]); wordSize = Integer.valueOf(args[0]); SequenceType refSeqType = null; querySeqType = SeqUtils.guessSequenceType(queryFile); queryReader = new SequenceReader(queryFile); if (querySeqType == SequenceType.Protein) { throw new Exception("Expected nucl query sequences"); } refSeqType = SeqUtils .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2])); translQuery = refSeqType == SequenceType.Protein; if (translQuery && wordSize % 3 != 0) { throw new Exception("Word size must be a multiple of 3 for nucl ref seqs"); } int trieWordSize; if (translQuery) { trieWordSize = wordSize / 3; } else { trieWordSize = wordSize; } kmerTrie = new KmerTrie(trieWordSize, translQuery); for (int index = 2; index < args.length; index++) { String refName; String refFileName = args[index]; if (refFileName.contains("=")) { String[] lexemes = refFileName.split("="); refName = lexemes[0]; refFileName = lexemes[1]; } else { String tmpName = new File(refFileName).getName(); if (tmpName.contains(".")) { refName = tmpName.substring(0, tmpName.lastIndexOf(".")); } else { refName = tmpName; } } File refFile = new File(refFileName); if (refSeqType != SeqUtils.guessSequenceType(refFile)) { throw new Exception( "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile) + " sequences but expected " + refSeqType + " sequences"); } SequenceReader seqReader = new SequenceReader(refFile); Sequence seq; while ((seq = seqReader.readNextSequence()) != null) { if (seq.getSeqName().startsWith("#")) { continue; } if (alignedSeqs) { kmerTrie.addModelSequence(seq, refLabels.size()); } else { kmerTrie.addSequence(seq, refLabels.size()); } } seqReader.close(); refLabels.add(refName); } } catch (Exception e) { new HelpFormatter().printHelp("KmerSearch <word_size> <query_file> [name=]<ref_file> ...", options); System.err.println(e.getMessage()); e.printStackTrace(); System.exit(1); throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables } long startTime = System.currentTimeMillis(); long seqCount = 0; final int maxTasks = 25000; /* * if (args.length == 4) { maxThreads = Integer.valueOf(args[3]); } else { */ //} System.err.println("Starting kmer mapping at " + new Date()); System.err.println("* Number of threads: " + maxThreads); System.err.println("* References: " + refLabels); System.err.println("* Reads file: " + queryFile); System.err.println("* Kmer length: " + kmerTrie.getWordSize()); final AtomicInteger processed = new AtomicInteger(); final AtomicInteger outstandingTasks = new AtomicInteger(); ExecutorService service = Executors.newFixedThreadPool(maxThreads); Sequence querySeq; while ((querySeq = queryReader.readNextSequence()) != null) { seqCount++; String seqString = querySeq.getSeqString(); if (seqString.length() < 3) { System.err.println("Sequence " + querySeq.getSeqName() + "'s length is less than 3"); continue; } final Sequence threadSeq = querySeq; Runnable r = new Runnable() { public void run() { try { processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, false); processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, true); } catch (IOException e) { throw new RuntimeException(e); } processed.incrementAndGet(); outstandingTasks.decrementAndGet(); } }; outstandingTasks.incrementAndGet(); service.submit(r); while (outstandingTasks.get() >= maxTasks) ; if ((processed.get() + 1) % 1000000 == 0) { System.err.println("Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); } } service.shutdown(); service.awaitTermination(1, TimeUnit.DAYS); System.err.println("Finished Processed " + processed + " sequences in " + (System.currentTimeMillis() - startTime) + " ms"); out.close(); }
From source file:Main.java
public Main(int poolSize) { threads = Executors.newFixedThreadPool(poolSize); }
From source file:Main.java
/** * Runs and blocking waits for the given callable to finish for the given * time. Returns <code>null</code> if timeouts waiting for callable value. * //from w w w .jav a 2 s . c om * @param millisTimeout * @param callable * @return */ public static <R> R runWithTimeout(long millisTimeout, Callable<R> callable) { ExecutorService singleThreadExecutor = Executors.newFixedThreadPool(1); Future<R> future = singleThreadExecutor.submit(callable); try { return future.get(millisTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { } finally { singleThreadExecutor.shutdown(); } return null; }
From source file:Main.java
public void processUsers(int numOfWorkerThreads) { ExecutorService threadPool = Executors.newFixedThreadPool(numOfWorkerThreads); int chunk = itemsToBeProcessed.length / numOfWorkerThreads; int start = 0; List<Future> tasks = new ArrayList<Future>(); for (int i = 0; i < numOfWorkerThreads; i++) { tasks.add(threadPool.submit(new WorkerThread(start, start + chunk))); start = start + chunk;// w w w .ja v a 2s. c o m } // join all worker threads to main thread for (Future f : tasks) { try { f.get(); } catch (Exception e) { e.printStackTrace(); } } threadPool.shutdown(); while (!threadPool.isTerminated()) { } }
From source file:Main.java
public static Set<String> findMatches(List<String> searchList, Set<String> targetSet) throws InterruptedException, ExecutionException { Set<String> locatedMatchSet = new HashSet<String>(); int threadCount = Runtime.getRuntime().availableProcessors(); List<List<String>> partitionList = getChunkList(searchList, threadCount); if (partitionList.size() == 1) { // if we only have one "chunk" then don't bother with a thread-pool locatedMatchSet = new ListSearcher(searchList, targetSet).call(); } else {/* w w w . ja va 2s .co m*/ ExecutorService executor = Executors.newFixedThreadPool(threadCount); CompletionService<Set<String>> completionService = new ExecutorCompletionService<Set<String>>(executor); for (List<String> chunkList : partitionList) completionService.submit(new ListSearcher(chunkList, targetSet)); for (int x = 0; x < partitionList.size(); x++) { Set<String> threadMatchSet = completionService.take().get(); locatedMatchSet.addAll(threadMatchSet); } executor.shutdown(); } return locatedMatchSet; }
From source file:com.evolveum.midpoint.web.component.GuiComponents.java
public static void init() { EXECUTOR = Executors.newFixedThreadPool(10); }
From source file:ejp.examples.MultiThreadedWithConnectionPooling.java
static void execute(final DatabaseManager dbm) throws DatabaseException, InterruptedException { long time = System.currentTimeMillis(); ExecutorService exec = Executors.newFixedThreadPool(100); System.out.println("\n\nWorking ..."); Runnable runnable = new Runnable() { public void run() { for (int t = 0; t < 100; t++) { try { new UpdateManager(dbm) { public void run() throws DatabaseException { for (int j = 0; j < 100; j++) { saveObject(new Dog(String.valueOf(Count.get()), Count.get())); Count.count(); }// w ww .ja va2 s .c om } }.executeBatchUpdates(); } catch (DatabaseException e) { e.printStackTrace(); } } } }; for (int i = 0; i < 100; i++) { exec.execute(runnable); } exec.shutdown(); exec.awaitTermination(100, TimeUnit.SECONDS); time = (System.currentTimeMillis() - time) / 1000; System.out.println("\n\n" + Count.count + " dogs added to database in " + time + " seconds"); Long count = ((Collection<Long>) dbm.executeQuery(new ArrayList<Long>(), true, "select count(*) from dog")) .toArray(new Long[1])[0]; System.out.println("select count(*) from dog = " + count); }
From source file:com.stb.async.ParallelExecutionProcess.java
/** * * @param processList/*from ww w. j a v a 2 s . c o m*/ */ public void initiateDecode(List processList) { Date startTime = new java.util.Date(); System.out.println("Start Work" + startTime); ExecutorService es = Executors.newFixedThreadPool(10); Collections.sort(processList, new ProcessCompare()); List<Future> futures = new ArrayList<>(); for (Iterator it = processList.iterator(); it.hasNext();) { Process e = (Process) it.next(); workerId = processList.indexOf(e); System.out.println("* Start Decode process " + processList.indexOf(e)); futures.add(es.submit(() -> { new DecodedSTBProcesses((Process) processList.get(ParallelExecutionProcess.workerId)).doWork(); return null; })); } es.shutdown(); System.out.println( "... The Process is under execution! Using CPU core which are available, wait while work is being done...."); int ctr = 0; for (Future future : futures) { try { future.get(); // blocking call, explicitly waiting for the response from a specific task, not necessarily the first task that is completed System.out.println("** Response of process " + ++ctr + " is in."); } catch (InterruptedException | ExecutionException e) { } } Date endTime = new java.util.Date(); System.out.println("End work at " + endTime); System.out.println("Total decoding took " + new Double(0.001 * (endTime.getTime() - startTime.getTime())) + " seconds"); System.exit(0); }
From source file:de.ii.xtraplatform.ogc.csw.client.CSWRequest.java
public CSWRequest(CSWAdapter csw, CSWOperation operation) { this.csw = csw; this.operation = operation; this.pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1)); }