List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:com.miz.mizuu.SearchForNetworkShares.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_list); pbar = (ProgressBar) findViewById(R.id.progressBar); status = (TextView) findViewById(R.id.overviewMessage); list = (ListView) findViewById(R.id.listView1); adapter = new NetworkAdapter(); list.setAdapter(adapter);/*from w ww.j a v a2s. c o m*/ list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { sendBroadcast(arg2); } }); new Thread() { @Override public void run() { ExecutorService executor = Executors.newFixedThreadPool(10); for (int dest = 0; dest < 255; dest++) { String host = "192.168.1." + dest; executor.execute(pingRunnable(host)); } executor.shutdown(); try { executor.awaitTermination(60 * 1000, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { } if (running) runOnUiThread(new Runnable() { @Override public void run() { pbar.setVisibility(View.GONE); status.setVisibility(View.GONE); } }); } }.start(); }
From source file:org.hibernate.search.test.performance.scenario.TestExecutor.java
private void scheduleTasksAndStart(TestScenarioContext ctx, long cyclesCount) { ExecutorService executor = newAutoStoppingErrorReportingThreadPool(ctx); for (int i = 0; i < cyclesCount; i++) { for (Runnable task : ctx.tasks) { executor.execute(task); }/*from w ww . j av a 2 s.c om*/ } try { ctx.executionStopWatch.start(); ctx.startSignal.countDown(); executor.shutdown(); executor.awaitTermination(1, TimeUnit.DAYS); ctx.executionStopWatch.stop(); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:lohbihler.process.epoll.ProcessMonitor.java
public ProcessMonitor(final Process process, final ExecutorService executorService, final long timeout) throws InterruptedException { final InputReader out = new InputReader(process.getInputStream()); final InputReader err = new InputReader(process.getErrorStream()); executorService.execute(out); executorService.execute(err);//from w w w . j av a 2 s . co m ProcessTimeout processTimeout = null; if (timeout > 0) { processTimeout = new ProcessTimeout(process, timeout); executorService.execute(processTimeout); } process.waitFor(); out.join(); err.join(); process.destroy(); // If we've made it this far, the process exited properly, so kill the // timeout thread if it exists. if (processTimeout != null) processTimeout.interrupt(); this.out = out.getInput(); this.err = err.getInput(); }
From source file:com.alibaba.wasp.master.GeneralBulkAssigner.java
@Override protected void populatePool(ExecutorService pool) { this.pool = pool; // shut it down later in case some assigner hangs for (Map.Entry<ServerName, List<EntityGroupInfo>> e : this.bulkPlan.entrySet()) { pool.execute(new SingleServerBulkAssigner(e.getKey(), e.getValue(), this.assignmentManager, this.failedPlans)); }//from w w w . j av a 2 s .co m }
From source file:org.apache.reef.vortex.evaluator.VortexWorker.java
/** * Starts the scheduler & executor and waits until termination. *//*ww w. j ava 2s .c om*/ @Override public byte[] call(final byte[] memento) throws Exception { final ExecutorService schedulerThread = Executors.newSingleThreadExecutor(); final ExecutorService commandExecutor = Executors.newFixedThreadPool(numOfThreads); // Scheduling thread starts schedulerThread.execute(new Runnable() { @Override public void run() { while (true) { // Scheduler Thread: Pick a command to execute (For now, simple FIFO order) final byte[] message; try { message = pendingRequests.takeFirst(); } catch (InterruptedException e) { throw new RuntimeException(e); } // Scheduler Thread: Pass the command to the worker thread pool to be executed commandExecutor.execute(new Runnable() { @Override public void run() { // Command Executor: Deserialize the command final VortexRequest vortexRequest = (VortexRequest) SerializationUtils .deserialize(message); switch (vortexRequest.getType()) { case ExecuteTasklet: final TaskletExecutionRequest taskletExecutionRequest = (TaskletExecutionRequest) vortexRequest; try { // Command Executor: Execute the command final Serializable result = taskletExecutionRequest.execute(); // Command Executor: Tasklet successfully returns result final WorkerReport report = new TaskletResultReport<>( taskletExecutionRequest.getTaskletId(), result); workerReports.addLast(SerializationUtils.serialize(report)); } catch (Exception e) { // Command Executor: Tasklet throws an exception final WorkerReport report = new TaskletFailureReport( taskletExecutionRequest.getTaskletId(), e); workerReports.addLast(SerializationUtils.serialize(report)); } heartBeatTriggerManager.triggerHeartBeat(); break; default: throw new RuntimeException("Unknown Command"); } } }); } } }); terminated.await(); return null; }
From source file:org.apache.hadoop.hbase.master.GeneralBulkAssigner.java
@Override protected void populatePool(ExecutorService pool) { this.pool = pool; // shut it down later in case some assigner hangs for (Map.Entry<ServerName, List<HRegionInfo>> e : this.bulkPlan.entrySet()) { pool.execute(new SingleServerBulkAssigner(e.getKey(), e.getValue(), this.assignmentManager, this.failedPlans)); }//from www. j a v a 2 s. c o m }
From source file:com.tenforce.lodms.extractors.CkanHarvester.java
public void harvest(List<String> datasetIds) throws RDFHandlerException, ExtractException, DatatypeConfigurationException { if (datasetIds.isEmpty()) { throw new ExtractException("no datasets specified"); }//from w w w. j av a 2 s.c o m if (enableProvenance) addCatalogProvenance(); MapToRdfConverter converter = new MapToRdfConverter(predicatePrefix, ignoredKeys, handler); ExecutorService executorService = Executors.newFixedThreadPool(5); CountDownLatch barrier = new CountDownLatch(datasetIds.size()); Catalog catalog = new Catalog(baseUri, subjectPrefix); try { for (String datasetId : datasetIds) { executorService.execute(new DataSetHarvester(catalog, converter, handler, apiUri, datasetId, barrier, warnings, httpMethod)); } executorService.shutdown(); barrier.await(); } catch (Exception e) { executorService.shutdownNow(); throw new ExtractException(e.getMessage(), e); } }
From source file:rapture.kernel.EnvironmentApiImpl.java
private Map<String, String> processGetOrPost(List<String> appNames, String path, String json) { Map<String, String> ret = new HashMap<>(); Map<String, JmxApp> apps; try {//from w w w . ja va2s . c om apps = JmxAppCache.getInstance().get(); } catch (ExecutionException e) { log.error("Failed to update JmxApp cache", e); return ret; } log.debug("Apps are: " + apps.toString()); ExecutorService executor = Executors.newFixedThreadPool(apps.size()); for (Map.Entry<String, JmxApp> entry : apps.entrySet()) { final String appName = entry.getKey(); executor.execute(new Runnable() { @Override public void run() { log.debug(String.format("Executing for appName [%s]", appName)); if (checkApp(appName, appNames)) { JmxApp app = entry.getValue(); HttpResponse<JsonNode> res; try { if (StringUtils.isNotBlank(json)) { res = Unirest.post(app.getUrl()).body(json).asJson(); } else { res = Unirest.get(String.format("%s/%s", app.getUrl(), path)).asJson(); } ret.put(entry.getKey(), IOUtils.toString(res.getRawBody())); res.getRawBody().close(); } catch (UnirestException | IOException e) { log.error(String.format("Error accessing %s/%s", app.getUrl(), path), e); JmxAppCache.getInstance().invalidate(); } } } }); } executor.shutdown(); try { executor.awaitTermination(4, TimeUnit.SECONDS); } catch (InterruptedException e) { log.error("Could not wait for executor shutdown", e); } log.debug("Returned result size is: " + ret.size()); return ret; }
From source file:org.biopax.validator.impl.ValidatorImpl.java
private void execute(ExecutorService exec, final Rule rule, final Validation validation, final Object obj) { exec.execute(new Runnable() { @SuppressWarnings("unchecked") //obj can be either Model or a BPE public void run() { try { if (rule.canCheck(obj)) rule.check(validation, obj); } catch (Throwable t) { //if we're here, there is probably a bug in the rule or validator! String id = validation.identify(obj); log.fatal(rule + ".check(" + id + ") threw the exception: " + t.toString(), t); // anyway, report it almost normally (for a user to see this in the results too) validation.addError(//from w w w.j a v a2s. c o m utils.createError(id, "exception", rule.getClass().getName(), null, false, t)); } } }); }
From source file:org.opennms.features.newts.converter.rrd.converter.JRobinConverter.java
private void consolidateRrd(final ExecutorService executor, final File rrdFile) { m_total.incrementAndGet();// w w w . ja v a 2 s . c o m executor.execute(new JRobinConsolidationRunnable(rrdFile, this)); }