List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:name.yumao.douyu.http.PlaylistDownloader.java
public static void go(/**final String name,*/ final String num) { // PlaylistDownloader loader = new PlaylistDownloader("http://"); ExecutorService service = Executors.newCachedThreadPool(); // GetList producer = new GetList(); roomnum = num;//w w w .j av a2 s .com // Down consumer = new Down(); // final String id = ""; service.execute(new Runnable() { public void run() { while (true) { // // ZhanqiApiVo vo = HttpClientFromZhanqi.QueryZhanqiDownloadUrl(inNum.getText() ); try { String url = HttpClientFromDouyu.getHTML5DownUrl(num); if (!url.equals("")) { // fetchsubPlaylist(new URL(url)); } else { logger.info("error"); outFile = getPath(); } } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // // } // try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); service.execute(new Runnable() { public void run() { while (true) { logger.info("down ......................"); URL down = null; while (true) { try { down = basket.poll(); logger.debug("down:" + down); if (down != null) downloadInternal(down); Thread.sleep(500); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //download("outtest"); } } }); }
From source file:org.apache.kylin.storage.hbase.util.DeployCoprocessorCLI.java
private static List<String> resetCoprocessorOnHTables(final Admin hbaseAdmin, final Path hdfsCoprocessorJar, List<String> tableNames) throws IOException { List<String> processedTables = Collections.synchronizedList(new ArrayList<String>()); ExecutorService coprocessorPool = Executors .newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); CountDownLatch countDownLatch = new CountDownLatch(tableNames.size()); for (final String tableName : tableNames) { coprocessorPool.execute(new ResetCoprocessorWorker(countDownLatch, hbaseAdmin, hdfsCoprocessorJar, tableName, processedTables)); }/*from w w w .jav a2 s . com*/ try { countDownLatch.await(); } catch (InterruptedException e) { logger.error("reset coprocessor failed: ", e); } coprocessorPool.shutdown(); return processedTables; }
From source file:edu.stanford.epad.epadws.queries.Dcm4CheeQueries.java
public static DICOMElementList getDICOMElementsFromWADO(String studyUID, String seriesUID, String imageUID, SegmentedProperty catTypeProp) { String catCode = ""; String typeCode = ""; DICOMElementList dicomElementList = new DICOMElementList(); DICOMElementList dicomElementListNoSkip = new DICOMElementList(); boolean skipThumbnail = false; try {/* w w w.ja v a 2 s. com*/ File temporaryDICOMFile = File.createTempFile(imageUID, ".tmp"); int wadoStatusCode = DCM4CHEEUtil.downloadDICOMFileFromWADO(studyUID, seriesUID, imageUID, temporaryDICOMFile); if (wadoStatusCode == HttpServletResponse.SC_OK) { File tempTag = File.createTempFile(imageUID, "_tag.tmp"); ExecutorService taskExecutor = Executors.newFixedThreadPool(4); taskExecutor.execute(new DicomHeadersTask(seriesUID, temporaryDICOMFile, tempTag)); taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); BufferedReader tagReader = null; try { String dicomElementString; FileReader tagFileReader = new FileReader(tempTag.getAbsolutePath()); tagReader = new BufferedReader(tagFileReader); skipThumbnail = false; String currentSequence = ""; while ((dicomElementString = tagReader.readLine()) != null) { if (dicomElementString.contains("(0009,1110)")) // hard code for now TODO:??? skipThumbnail = true; if (dicomElementString.contains("(FFFE,E0DD)")) skipThumbnail = false; int sequence = dicomElementString.indexOf("SQ #-1"); if (sequence != -1) currentSequence = dicomElementString.substring(sequence + 7); if (dicomElementString.contains("Sequence Delimitation Item")) currentSequence = ""; DICOMElement dicomElement = decodeDICOMElementString(dicomElementString); DICOMElement dicomElementNoSkip = decodeDICOMElementString(dicomElementString); if (dicomElement != null) { if (!skipThumbnail) { dicomElement.parentSequenceName = currentSequence; dicomElementList.addDICOMElement(dicomElement); if (dicomElementString.contains("(0008,0100)")) { if (dicomElement.parentSequenceName != null && dicomElement.parentSequenceName.equalsIgnoreCase( "Segmented Property Category Code Sequence"))//category code { catCode = dicomElement.value.trim(); log.info("cat code is " + catCode); } else if (dicomElement.parentSequenceName != null && dicomElement.parentSequenceName .equalsIgnoreCase("Segmented Property Type Code Sequence"))//category code { typeCode = dicomElement.value.trim(); log.info("type code is " + typeCode); } } } //make a list with all the skip items //at the end if the skip is not closed then use this list else { log.warning("Warning: skip sequence. skipping " + dicomElementString); dicomElementNoSkip.parentSequenceName = currentSequence; dicomElementListNoSkip.addDICOMElement(dicomElementNoSkip); } } else { //too much log // log.warning("Warning: could not decode DICOM element " + dicomElementString + ""); } } } finally { IOUtils.closeQuietly(tagReader); try { temporaryDICOMFile.delete(); tempTag.delete(); } catch (Exception x) { } ; } } catch (InterruptedException e) { Thread.currentThread().interrupt(); log.warning("DICOM headers task for series " + seriesUID + " interrupted!"); } } else { log.warning("Error invoking dcm4chee to get DICOM headers for series " + seriesUID + "; status code=" + wadoStatusCode); } } catch (IOException e) { log.warning("IOException retrieving DICOM headers for image " + imageUID + " in series " + seriesUID, e); } try { if (catTypeProp != null && !catCode.equals("") && !typeCode.equals("")) { SegmentedPropertyHelper helper = new SegmentedPropertyHelper(); SegmentedProperty prop = helper.getProperty(catCode, typeCode); if (prop != null) { catTypeProp.copyValuesFrom(prop); } else { log.info("Category-type pair not found"); } } } catch (Exception ex) { log.warning("Exception in getting category type ", ex); } if (skipThumbnail) { log.warning("End of skip not found returning noskip data. "); return dicomElementListNoSkip; } return dicomElementList; }
From source file:agileinterop.AgileInterop.java
private static JSONObject getPSRData(String body) throws ParseException, InterruptedException, APIException { // Parse body as JSON JSONParser parser = new JSONParser(); JSONArray jsonBody = (JSONArray) parser.parse(body); Map<String, Object> data = new HashMap<>(); class GetObjectData implements Runnable { private String psrNumber; private Map<String, Object> data; private IServiceRequest psr; public GetObjectData(String psrNumber, Map<String, Object> data) throws APIException, InterruptedException { this.psrNumber = psrNumber; this.data = data; psr = (IServiceRequest) Agile.session.getObject(IServiceRequest.OBJECT_TYPE, psrNumber); }/*from ww w .j a va 2 s .c om*/ @Override public void run() { this.data.put(psrNumber, new HashMap<String, Object>()); try { if (psr != null) { getCellValues(); getAttachments(); getHistory(); } } catch (APIException ex) { Logger.getLogger(AgileInterop.class.getName()).log(Level.SEVERE, null, ex); } } private void getCellValues() throws APIException { Map<String, Object> cellValues = new HashMap<>(); long startTime = System.currentTimeMillis(); // Get cell values ICell[] cells = psr.getCells(); for (ICell cell : cells) { if (cell.getDataType() == DataTypeConstants.TYPE_DATE) { if (cell.getValue() != null) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz"); sdf.setTimeZone(TimeZone.getTimeZone("Europe/London")); cellValues.put(cell.getName(), sdf.format((Date) cell.getValue())); } else { cellValues.put(cell.getName(), cell.toString()); } } else { cellValues.put(cell.getName(), cell.toString()); } } long endTime = System.currentTimeMillis(); String logMessage = String.format("%s: getCellValues executed in %d milliseconds", psrNumber, endTime - startTime); System.out.println(logMessage); ((HashMap<String, Object>) this.data.get(psrNumber)).put("cellValues", cellValues); } private void getAttachments() throws APIException { List<Map<String, String>> attachments = new ArrayList<>(); long startTime = System.currentTimeMillis(); // Get attachments information ITable table = psr.getTable("Attachments"); ITwoWayIterator tableIterator = table.getTableIterator(); while (tableIterator.hasNext()) { IRow row = (IRow) tableIterator.next(); Map<String, String> attachment = new HashMap<>(); ICell[] cells = row.getCells(); for (ICell cell : cells) { if (cell.getDataType() == DataTypeConstants.TYPE_DATE) { if (cell.getValue() != null) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz"); sdf.setTimeZone(TimeZone.getTimeZone("Europe/London")); attachment.put(cell.getName(), sdf.format((Date) cell.getValue())); } else { attachment.put(cell.getName(), cell.toString()); } } else { attachment.put(cell.getName(), cell.toString()); } } attachments.add(attachment); } long endTime = System.currentTimeMillis(); String logMessage = String.format("%s: getAttachments executed in %d milliseconds", psrNumber, endTime - startTime); System.out.println(logMessage); ((HashMap<String, Object>) this.data.get(psrNumber)).put("attachments", attachments); } private void getHistory() throws APIException { List<Map<String, String>> histories = new ArrayList<>(); long startTime = System.currentTimeMillis(); // Get history information ITable table = psr.getTable("History"); ITwoWayIterator tableIterator = table.getTableIterator(); while (tableIterator.hasNext()) { IRow row = (IRow) tableIterator.next(); Map<String, String> history = new HashMap<>(); ICell[] cells = row.getCells(); for (ICell cell : cells) { if (cell.getDataType() == DataTypeConstants.TYPE_DATE) { if (cell.getValue() != null) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a zz"); sdf.setTimeZone(TimeZone.getTimeZone("Europe/London")); history.put(cell.getName(), sdf.format((Date) cell.getValue())); } else { history.put(cell.getName(), cell.toString()); } } else { history.put(cell.getName(), cell.toString()); } } histories.add(history); } long endTime = System.currentTimeMillis(); String logMessage = String.format("%s: getHistory executed in %d milliseconds", psrNumber, endTime - startTime); System.out.println(logMessage); ((HashMap<String, Object>) this.data.get(psrNumber)).put("history", histories); } } synchronized (data) { // Do something funky with the first one Thread t = new Thread(new GetObjectData(jsonBody.get(0).toString(), data)); t.start(); t.join(); ExecutorService executor = Executors.newFixedThreadPool(10); for (Object object : jsonBody.subList(1, jsonBody.size() - 1)) { executor.execute(new Thread(new GetObjectData(object.toString(), data))); } executor.shutdown(); while (!executor.isTerminated()) { } } JSONObject obj = new JSONObject(); obj.put("data", data); return obj; }
From source file:be.solidx.hot.utils.IOUtils.java
public static Promise<byte[], Exception, Void> asyncRead(final HttpServletRequest req, final ExecutorService executorService, final ExecutorService promiseResolver) { final DeferredObject<byte[], Exception, Void> deferredObject = new DeferredObject<>(); try {/* w w w.ja va 2 s.c om*/ final ServletInputStream servletInputStream = req.getInputStream(); servletInputStream.setReadListener(new ReadListener() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @Override public void onError(final Throwable t) { promiseResolver.execute(new Runnable() { @Override public void run() { deferredObject.reject(new Exception(t)); } }); } @Override public void onDataAvailable() throws IOException { executorService.execute(new Runnable() { @Override public void run() { byte b[] = new byte[2048]; int len = 0; try { while (servletInputStream.isReady() && (len = servletInputStream.read(b)) != -1) { baos.write(b, 0, len); } } catch (IOException e) { LOGGER.error("", e); } } }); } @Override public void onAllDataRead() throws IOException { promiseResolver.execute(new Runnable() { @Override public void run() { deferredObject.resolve(baos.toByteArray()); } }); } }); } catch (final IOException e2) { promiseResolver.execute(new Runnable() { @Override public void run() { deferredObject.reject(new Exception(e2)); } }); } catch (final IllegalStateException exception) { promiseResolver.execute(new Runnable() { @Override public void run() { deferredObject.resolve("".getBytes()); } }); } // executorService.execute(new Runnable() { // @Override // public void run() { // final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // try { // IOUtils.toOutputStreamBuffered(req.getInputStream(), outputStream); // promiseResolver.execute(new Runnable() { // @Override // public void run() { // deferredObject.resolve(outputStream.toByteArray()); // } // }); // } catch (final IOException e) { // promiseResolver.execute(new Runnable() { // @Override // public void run() { // deferredObject.reject(e); // } // }); // } // } // }); return deferredObject.promise(); }
From source file:org.wso2.carbon.bpmn.people.substitution.UserSubstitutionUtils.java
private static void executeInThreadPool(Runnable runnable) { ExecutorService pool = Executors.newCachedThreadPool(); pool.execute(runnable); }
From source file:org.apache.accumulo.test.functional.FunctionalTestUtils.java
public static void createRFiles(final AccumuloClient c, final FileSystem fs, String path, int rows, int splits, int threads) throws Exception { fs.delete(new Path(path), true); ExecutorService threadPool = Executors.newFixedThreadPool(threads); final AtomicBoolean fail = new AtomicBoolean(false); for (int i = 0; i < rows; i += rows / splits) { TestIngest.IngestParams params = new TestIngest.IngestParams(c.properties()); params.outputFile = String.format("%s/mf%s", path, i); params.random = 56;// w w w.ja va 2 s . c o m params.timestamp = 1; params.dataSize = 50; params.rows = rows / splits; params.startRow = i; params.cols = 1; threadPool.execute(() -> { try { TestIngest.ingest(c, fs, params); } catch (Exception e) { fail.set(true); } }); } threadPool.shutdown(); threadPool.awaitTermination(1, TimeUnit.HOURS); assertFalse(fail.get()); }
From source file:com.ubipass.middleware.ems.EMSUtil.java
/** * Startup EMS./*from ww w . j av a2 s . c o m*/ * * @return boolean */ public static synchronized boolean startupEMS() { // check license long expiredDate; try { expiredDate = EMSUtil.checkLicence(); } catch (Exception e) { SystemLogger.error("System cannot find valid license"); return false; } if (expiredDate < new Date().getTime()) { SystemLogger.error("License has expired"); return false; } if (observation == null) { SystemLogger.error("[EMS] Cannot setup database connection"); return false; } if (isRunning) { // EMS is running return true; } SystemLogger.info("[EMS] EMS is starting..."); // update table observation try { observation.updateSystemRemoveTime(getLastShutdownTime()); } catch (Exception e) { SystemLogger.error("[EMS] Update table observation error: " + e.getMessage()); return false; } ExecutorService executor = ThreadPool.getInstance(); if (executor == null) { SystemLogger.error("[EMS] Cannot get an ExecutorService from thread pool"); return false; } try { // start event worker worker = new EventWorker(); executor.execute(worker); } catch (Exception e) { SystemLogger.error("[EMS] Starting EventWorker error: " + e.getMessage()); return false; } try { timeUpdater = new TimeUpdater(expiredDate); // start time updater executor.execute(timeUpdater); } catch (Exception e) { SystemLogger.error("[EMS] Starting TimeUpdater error: " + e.getMessage()); return false; } // start all devices DeviceList.startupAllDevices(); SystemLogger.info("[EMS] EMS started successfully"); isRunning = true; return true; }
From source file:eu.stratosphere.nephele.streaming.taskmanager.chaining.TaskChain.java
public static Pair<TaskChain, TaskChain> splitAndAnnounceChain(TaskChain chain, int splitIndex, ExecutorService backgroundWorkers, final QosReporterConfigCenter configCenter) { final TaskChain newLeftChain = new TaskChain(); final TaskChain newRightChain = new TaskChain(); newLeftChain.tasksInChain.addAll(chain.tasksInChain.subList(0, splitIndex)); newRightChain.tasksInChain.addAll(chain.tasksInChain.subList(splitIndex, chain.tasksInChain.size())); newLeftChain.fixTasksChainedStatus(); newRightChain.fixTasksChainedStatus(); newLeftChain.taskControlFlowsUnderManipulation.set(true); newRightChain.taskControlFlowsUnderManipulation.set(true); backgroundWorkers.execute(new Runnable() { @Override/*from ww w . j av a 2s. co m*/ public void run() { try { ChainingUtil.unchainAndAnnounceTaskThreads(newLeftChain, newRightChain, configCenter); } catch (Exception e) { LOG.error("Error during chain construction.", e); } finally { newLeftChain.taskControlFlowsUnderManipulation.set(false); newRightChain.taskControlFlowsUnderManipulation.set(false); } } }); return Pair.of(newLeftChain, newRightChain); }
From source file:gda.device.detector.mythen.data.MythenDataFileUtils.java
/** * Reads the specified Mythen processed data files. * // www . j a v a 2 s . c o m * @param filenames * the names of the files to read * @return 3D double array of data */ public static double[][][] readMythenProcessedDataFiles(String filenames[]) { // 3D array of data; will be filled in by tasks (one task per file to be loaded) final double[][][] data = new double[filenames.length][][]; // thread pool for loading files // 4 threads seems to give good results ExecutorService executor = Executors.newFixedThreadPool(4); // create and execute a task for each file to be loaded for (int i = 0; i < filenames.length; i++) { final int index = i; final String filename = filenames[i]; Runnable r = new Runnable() { @Override public void run() { data[index] = readMythenProcessedDataFile(filename, false); } }; executor.execute(r); } // wait until executor has shut down executor.shutdown(); try { boolean terminated = executor.awaitTermination(1, TimeUnit.MINUTES); if (!terminated) { throw new Exception("Timed out waiting for files to load"); } } catch (Exception e) { throw new RuntimeException("Unable to load data", e); } return data; }