List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:com.haulmont.cuba.core.UniqueNumbersTest.java
@Test public void testConcurrentModification() throws Exception { int threadCnt = 8; ExecutorService executorService = Executors.newFixedThreadPool(threadCnt, new ThreadFactoryBuilder().setNameFormat("T%d").build()); final Action[] actions = { new SleepAction(), new GetNumberAction(), new SetNumberAction(), new DeleteSequenceAction() }; for (int i = 0; i < threadCnt; i++) { final int finalI = i; executorService.submit(new Runnable() { int runnableNo = finalI; @Override// w w w . j ava 2 s . c o m public void run() { ThreadLocalRandom random = ThreadLocalRandom.current(); for (int i = 0; i < 10; i++) { int action = random.nextInt(0, 4); System.out.println( "Runnable " + runnableNo + " iteration " + i + " action " + actions[action]); try { int seqN = actions[action].perform(runnableNo); actions[action].success(runnableNo, seqN); } catch (Exception e) { if (e instanceof IllegalStateException && StringUtils.contains(e.getMessage(), "Attempt to delete")) { System.err.println(e.getMessage()); continue; } System.err.println(e.getMessage()); exceptionCnt.incrementAndGet(); } } finishedThreads.incrementAndGet(); } }); } while (finishedThreads.get() < threadCnt) { System.out.println("Waiting..."); TimeUnit.MILLISECONDS.sleep(200); } assertEquals(exceptionCnt.get(), 0); }
From source file:com.easarrive.quartz.aws.service.impl.PythonWordSegmenterService.java
/** * {@inheritDoc}//from www . java 2 s . c o m */ @Override public void teachFromDatabase(final String wordSegmenterURL, String dbName, String tableName, String taskName) { //? List<CronTask> cronTaskList = cronTaskReadMapper.find(dbName, tableName, taskName); CronTask dbCronTask = null; //????? if (cronTaskList != null && cronTaskList.size() > 0) { dbCronTask = cronTaskList.get(0); if (cronTaskList.size() > 1) { for (int index = 1; index < cronTaskList.size(); index++) { CronTask deleteCronTask = cronTaskList.get(index); Long count = cronTaskWriteMapper.delete(deleteCronTask.getId()); if (logger.isDebugEnabled()) { logger.debug("Python ???? {}", count); } } } } //? Long tableUpdateTime = null; if (dbCronTask != null) { tableUpdateTime = dbCronTask.getTableUpdateTime(); dbCronTask.setLastExecTime(dbCronTask.getExecTime()); dbCronTask.setExecTime(System.currentTimeMillis()); dbCronTask.setUpdateTime(System.currentTimeMillis()); } if (tableUpdateTime == null) { tableUpdateTime = -1L; } // List<GoodsTags> goodsTagsList = goodsTagsReadMapper.getAll(tableUpdateTime); if (goodsTagsList == null || goodsTagsList.size() < 1) { if (logger.isInfoEnabled()) { logger.info("Python ?"); } return; } // ExecutorService executorService = Executors.newCachedThreadPool(); List<Future<Map<Integer, Boolean>>> futureList = new ArrayList<Future<Map<Integer, Boolean>>>(); for (final GoodsTags tags : goodsTagsList) { if (tags == null) { continue; } Future<Map<Integer, Boolean>> future = executorService.submit(new Callable<Map<Integer, Boolean>>() { @Override public Map<Integer, Boolean> call() throws Exception { Map<Integer, Boolean> result = new HashMap<Integer, Boolean>(); result.put(tags.getId(), null); if (StringUtil.isEmpty(tags.getTag())) { return result; } try { String wordSegmenterURLFinal = String.format("%s%s", wordSegmenterURL, URLEncoder.encode(tags.getTag(), Constant.Charset.UTF8)); HttpResponse httpResponse = HttpClientUtil.get(wordSegmenterURLFinal, null); int statusCode = httpResponse.getStatusLine().getStatusCode(); result.put(tags.getId(), statusCode == 200); } catch (Exception e) { result.put(tags.getId(), false); } return result; } }); futureList.add(future); if (tableUpdateTime < tags.getAddTime()) { tableUpdateTime = tags.getAddTime(); } } //? for (Future<Map<Integer, Boolean>> future : futureList) { try { Map<Integer, Boolean> result = future.get(); if (logger.isInfoEnabled()) { logger.info("Python ?? {}", result); } } catch (InterruptedException e) { if (logger.isErrorEnabled()) { logger.error(e.getMessage(), e); } } catch (ExecutionException e) { if (logger.isErrorEnabled()) { logger.error(e.getMessage(), e); } } } // if (dbCronTask == null) { dbCronTask = new CronTask(0L, dbName, tableName, tableUpdateTime, taskName, System.currentTimeMillis(), System.currentTimeMillis(), System.currentTimeMillis(), System.currentTimeMillis(), 0); Long id = cronTaskWriteMapper.insert(dbCronTask); if (logger.isDebugEnabled()) { logger.debug(" Python ? {}", id); } } else { Long count = cronTaskWriteMapper.update(dbCronTask); if (logger.isDebugEnabled()) { logger.debug(" Python ?? {}", count); } } }
From source file:c3.ops.priam.resources.BackupServlet.java
/** * Convert SSTable2Json and search for given key *//*from w ww . j a va 2 s.c om*/ public void checkSSTablesForKey(String rowkey, String keyspace, String cf, String fileExtension, String jsonFilePath) throws Exception { try { logger.info("Starting SSTable2Json conversion ..."); //Setting timeout to 10 Mins long TIMEOUT_PERIOD = 10l; String unixCmd = formulateCommandToRun(rowkey, keyspace, cf, fileExtension, jsonFilePath); String[] cmd = { "/bin/sh", "-c", unixCmd.toString() }; final Process p = Runtime.getRuntime().exec(cmd); Callable<Integer> callable = new Callable<Integer>() { @Override public Integer call() throws Exception { int returnCode = p.waitFor(); return returnCode; } }; ExecutorService exeService = Executors.newSingleThreadExecutor(); try { Future<Integer> future = exeService.submit(callable); int returnVal = future.get(TIMEOUT_PERIOD, TimeUnit.MINUTES); if (returnVal == 0) logger.info("Finished SSTable2Json conversion and search."); else logger.error("Error occurred during SSTable2Json conversion and search."); } catch (TimeoutException e) { logger.error(ExceptionUtils.getStackTrace(e)); throw e; } finally { p.destroy(); exeService.shutdown(); } } catch (IOException e) { logger.error(ExceptionUtils.getStackTrace(e)); } }
From source file:bemap.SimpleSerial.java
public int searchDevicePort() throws InterruptedException { String[] portNames = SerialPortList.getPortNames(); if (SERIAL_DEBUG) BeMapEditor.mainWindow.append("\nFound ports: " + Arrays.toString(portNames)); int portNumber = -1; for (int i = 0; i < portNames.length; i++) { currentPortName = portNames[i];//from w w w . j ava2 s . co m ExecutorService executor = Executors.newSingleThreadExecutor(); Future<String> future = executor.submit(new PortSearchTask()); try { String returnPort = future.get(3, TimeUnit.SECONDS); if (!"NULL".equals(returnPort)) { portNumber = i; i = portNames.length; //quit loop } } catch (ExecutionException ex) { Logger.getLogger(SimpleSerial.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(SimpleSerial.class.getName()).log(Level.SEVERE, null, ex); if (SERIAL_DEBUG) BeMapEditor.mainWindow.append("\nOpening port " + currentPortName + " has timed out"); } executor.shutdownNow(); } if (portNumber < 0) { deviceConnected = false; return 0; } else { serialPortName = portNames[portNumber]; deviceConnected = true; if (SERIAL_DEBUG) BeMapEditor.mainWindow.append("\nDevice detected on port " + serialPortName); return 1; //success } }
From source file:com.chess.genesis.net.SyncClient.java
private void sync_active(final JSONObject json) { try {/* ww w. j a v a2 s. c o m*/ final ArrayList<String> list_need = getNeedList(json.getJSONArray("gameids")); final ExecutorService pool = Executors.newCachedThreadPool(); for (final String item : list_need) { if (error) return; final NetworkClient nc = new NetworkClient(context, handle); nc.game_info(item); pool.submit(nc); lock++; } // don't save time if only syncing active if (syncType == ACTIVE_SYNC) return; // Save sync time final long time = json.getLong("time"); final PrefEdit pref = new PrefEdit(context); pref.putLong(R.array.pf_lastgamesync, time); pref.commit(); } catch (final JSONException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.cloudera.oryx.rdf.common.tree.DecisionForest.java
public DecisionForest(final int numTrees, double fractionOfFeaturesToTry, final int minNodeSize, final double minInfoGainNats, final int suggestedMaxSplitCandidates, final int maxDepth, final double sampleRate, final ExampleSet examples) { Preconditions.checkArgument(numTrees > 1); final int numFeatures = examples.getNumFeatures(); Preconditions.checkArgument(fractionOfFeaturesToTry > 0.0 && fractionOfFeaturesToTry <= 1.0); final int featuresToTry = FastMath.max(1, (int) (fractionOfFeaturesToTry * numFeatures)); Preconditions.checkArgument(numFeatures >= 1); Preconditions.checkArgument(minNodeSize >= 1); Preconditions.checkArgument(minInfoGainNats >= 0.0); Preconditions.checkArgument(suggestedMaxSplitCandidates >= 1); Preconditions.checkArgument(maxDepth >= 1); Preconditions.checkArgument(sampleRate > 0.0 && sampleRate <= 1.0); weights = new double[numTrees]; Arrays.fill(weights, 1.0);// ww w.ja v a 2s. c o m evaluations = new double[numTrees]; Arrays.fill(evaluations, Double.NaN); final double[][] perTreeFeatureImportances = new double[numTrees][]; // Going to set an arbitrary upper bound on the training size of about 90% int maxFolds = FastMath.min(numTrees - 1, (int) (0.9 * numTrees)); // Going to set an arbitrary lower bound on the CV size of about 10% int minFolds = FastMath.max(1, (int) (0.1 * numTrees)); final int folds = FastMath.min(maxFolds, FastMath.max(minFolds, (int) (sampleRate * numTrees))); trees = new DecisionTree[numTrees]; ExecutorService executor = Executors.newFixedThreadPool(determineParallelism(trees.length)); try { Collection<Future<Object>> futures = Lists.newArrayListWithCapacity(trees.length); for (int i = 0; i < numTrees; i++) { final int treeID = i; futures.add(executor.submit(new Callable<Object>() { @Override public Void call() throws Exception { Collection<Example> allExamples = examples.getExamples(); int totalExamples = allExamples.size(); int expectedTrainingSize = (int) (totalExamples * sampleRate); int expectedCVSize = totalExamples - expectedTrainingSize; List<Example> trainingExamples = Lists.newArrayListWithExpectedSize(expectedTrainingSize); List<Example> cvExamples = Lists.newArrayListWithExpectedSize(expectedCVSize); for (Example example : allExamples) { if (IntMath.mod(IntMath.mod(example.hashCode(), numTrees) - treeID, numTrees) < folds) { trainingExamples.add(example); } else { cvExamples.add(example); } } Preconditions.checkState(!trainingExamples.isEmpty(), "No training examples sampled?"); Preconditions.checkState(!cvExamples.isEmpty(), "No CV examples sampled?"); trees[treeID] = new DecisionTree(numFeatures, featuresToTry, minNodeSize, minInfoGainNats, suggestedMaxSplitCandidates, maxDepth, examples.subset(trainingExamples)); log.info("Finished tree {}", treeID); ExampleSet cvExampleSet = examples.subset(cvExamples); double[] weightEval = Evaluation.evaluateToWeight(trees[treeID], cvExampleSet); weights[treeID] = weightEval[0]; evaluations[treeID] = weightEval[1]; perTreeFeatureImportances[treeID] = trees[treeID].featureImportance(cvExampleSet); log.info("Tree {} eval: {}", treeID, weightEval[1]); return null; } })); } ExecutorUtils.checkExceptions(futures); } finally { ExecutorUtils.shutdownNowAndAwait(executor); } featureImportances = new double[numFeatures]; for (double[] perTreeFeatureImporatance : perTreeFeatureImportances) { for (int i = 0; i < numFeatures; i++) { featureImportances[i] += perTreeFeatureImporatance[i]; } } for (int i = 0; i < numFeatures; i++) { featureImportances[i] /= numTrees; } }
From source file:com.github.kubernetes.java.client.live.KubernetesApiClientLiveTest.java
@Test public void testCreatePod() throws Exception { log.info("Testing Pods ...."); if (log.isDebugEnabled()) { log.debug("Creating a Pod " + pod); }//from w w w . j a va 2s . c o m Pod createPod = getClient().createPod(pod); assertEquals(pod.getId(), createPod.getId()); assertNotNull(getClient().getPod(pod.getId())); assertEquals("Waiting", createPod.getCurrentState().getStatus()); ExecutorService executor = Executors.newSingleThreadExecutor(); Future<Pod> future = executor.submit(new Callable<Pod>() { public Pod call() throws Exception { Pod newPod; do { log.info("Waiting for Pod to be ready: " + pod.getId()); Thread.sleep(1000); newPod = getClient().getPod(pod.getId()); StateInfo info = newPod.getCurrentState().getInfo("master"); if (info.getState("waiting") != null) { throw new RuntimeException("Pod is waiting due to " + info.getState("waiting")); } } while (!"Running".equals(newPod.getCurrentState().getStatus())); return newPod; } }); try { createPod = future.get(90, TimeUnit.SECONDS); } finally { executor.shutdownNow(); } assertNotNull(createPod.getCurrentState().getInfo("master").getState("running")); assertNotNull(createPod.getCurrentState().getNetInfo().getState("running")); // test recreation from same id try { getClient().createPod(pod); fail("Should have thrown exception"); } catch (Exception e) { // ignore } assertNotNull(getClient().getPod(pod.getId())); }
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Gets a {@link JSONArray} from the Google Maps API from a set of coordinates * @param lat//from w ww.java 2 s .co m * @param lon * @param maxResults * @return JSONArray of locations */ private static JSONArray getJSONArrayFromLocation(final double lat, final double lon, int maxResults) { ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<JSONArray> callable = new Callable<JSONArray>() { @Override public JSONArray call() throws IOException { String urlStr = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lon + "&sensor=false"; String response = ""; HttpClient client = new DefaultHttpClient(); HttpResponse hr = client.execute(new HttpGet(urlStr)); HttpEntity entity = hr.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); String buff = null; while ((buff = br.readLine()) != null) response += buff; br.close(); JSONArray responseArray = null; try { JSONObject jsonObject = new JSONObject(response); responseArray = jsonObject.getJSONArray("results"); } catch (JSONException e) { return null; } return responseArray; } }; Future<JSONArray> future = executor.submit(callable); JSONArray ret; try { ret = future.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block ret = null; } catch (ExecutionException e) { // TODO Auto-generated catch block ret = null; } return ret; }
From source file:cloudlens.notebook.JSInterpreter.java
public InterpreterResult interpret(Callable<BlockObject> task, CL cl) { if (cl.out instanceof ByteArrayOutputStream) { ((ByteArrayOutputStream) cl.out).reset(); }// w w w .j a v a 2s.c o m if (cl.err instanceof ByteArrayOutputStream) { ((ByteArrayOutputStream) cl.err).reset(); } final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r) { @Override public void interrupt() { stop(); } }; } }); cl.future = executor.submit(task); final Gson gson = new GsonBuilder().create(); try { final BlockObject obj = cl.future.get(); cl.outWriter.flush(); cl.errWriter.flush(); if (obj instanceof InterpreterResult) { return (InterpreterResult) obj; } if (cl.out instanceof ByteArrayOutputStream && ((ByteArrayOutputStream) cl.out).size() == 0) { if (null != obj && obj.isMapArray()) { final Map<String, Map<String, Object>> entries = obj.asMapArray(); cl.outWriter.print("%table\n"); int i = 0; for (final Map<?, ?> entry : entries.values()) { cl.outWriter.print("\n"); if (++i > maxResult) { cl.outWriter.println( "%html <font color=red>Results are limited by zeppelin.cloudlens.maxResult = " + maxResult + ".</font>"); break; } for (final Map.Entry<?, ?> field : entry.entrySet()) { cl.outWriter.print("%html <font color=blue>" + StringEscapeUtils.escapeHtml4(field.getKey().toString()) + "</font>:" + StringEscapeUtils.escapeHtml4(gson.toJson(field.getValue()).toString()) + "\t"); } } } else { cl.engine.bind("__Result__", obj); cl.engine.eval( "print(JSON.stringify(__Result__, function(key, val) { if (typeof val === 'function') return val + ''; return val; }, 2))"); } } // } } catch (final InterruptedException | ExecutionException e) { return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e)); } finally { cl.outWriter.flush(); cl.errWriter.flush(); executor.shutdownNow(); } return new InterpreterResult(Code.SUCCESS, cl.out.toString()); }
From source file:com.ebay.jetstream.event.processor.esper.ESPTest.java
public void testProcessor() { EsperProcessor processor = getProcessor("ESPTestProcessor"); ESPTestSink sink = new ESPTestSink(); List<EventSink> sinks = new ArrayList<EventSink>(); sinks.add(sink);//from ww w . j a v a 2s . c o m processor.setEventSinks(sinks); // TODO: start not exposed - processor.start(); // it was stopped while running previous test ExecutorService threadPool = Executors.newCachedThreadPool(new ESPTestThreadFactory()); Runnable runnables[] = new ESPTestRunnable[THREADS_NUM]; try { for (int i = 0; i < THREADS_NUM; i++) { runnables[i] = new ESPTestRunnable(processor, i); threadPool.submit(runnables[i]); } threadPool.shutdown(); threadPool.awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e) { fail("InterruptedException: " + e.getMessage()); } assertTrue("ExecutorService failed to shut down properly", threadPool.isShutdown()); // processor.stop(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertEquals(THREADS_NUM, sink.getCount()); testLogger.info("sink first, last = [" + sink.getIds().first() + "," + sink.getIds().last() + "]"); }