List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:at.sti2.sparkwave.SparkwaveKernel.java
/** * Kick off bootstrap// w w w .j ava 2 s. c o m */ private void bootstrap(ConfigurationModel sparkwaveConfig, List<File> patternFiles) { //Instantiate ExecutorService for SparkwaveProcessor // ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("Processor-%d").build(); // ExecutorService processorExecutor = Executors.newCachedThreadPool(tf); int patternFilesSize = patternFiles.size(); for (int i = 0; i < patternFilesSize; i++) { File patternFile = patternFiles.get(i); //Build triple pattern representation logger.info( "Parsing pattern file (" + (i + 1) + " of " + patternFilesSize + "): " + patternFile + "..."); SparkPatternParser patternParser = new SparkPatternParser(); SparkParserResult parserResult = null; try { parserResult = patternParser.parse(patternFile); } catch (IOException e) { logger.error("Could not open pattern file " + patternFile + " " + e.getMessage()); } catch (SparkParserException e) { logger.error("Could not parse the file " + patternFile + " " + e.getMessage()); } Pattern pattern = parserResult.getPattern(); logger.info("Parsed pattern:\n" + pattern); if (pattern != null) { addProcessorThread(pattern); // processorExecutor. // processorExecutor.shutdownNow(); //TODO generate pattern id (random? hash? int++?) for pattern, use name //TODO Associate: processor and thread, method to find corresponding sparkwave processor for name. HashMap<Name,<List<Processor,Thread>? //TODO 1) remove queue for pattern 2) shutdown processorThread //TODO Add GrizzlyServer and access SparkwaveKernel. //TODO getPatterns method //TODO Test to remove,add patterns!! /* pattern class * * variables: * filename * * method: * getContent() * */ /* this class variables: map<pattern,thread> list of patterns methods: * addProcessorThread(Pattern) * removeProcessorThread(Pattern) * getLoadedPatterns() * getLoadedPattern(name) * */ } } // for(Thread t : threads){ // try { // Thread.sleep(3000); // logger.info("interrupting "+t); // t.interrupt(); // t.join(); // logger.info("joined "+t); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } ThreadFactory threadFactoyServerSocket = new ThreadFactoryBuilder().setNameFormat("ServerSocket-%d") .build(); ExecutorService serverSocketExecutor = Executors.newSingleThreadExecutor(threadFactoyServerSocket); //One Server for all SparkwaveNetworks, acts as multiplexer serverSocketExecutor.execute(new ServerSocketThread(sparkwaveConfig, queues)); }
From source file:org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServerTest.java
@Test public void testConcurrentUpdate() throws Exception { TestServlet.clear();/*from ww w . j a v a 2 s . c om*/ String serverUrl = jetty.getBaseUrl().toString() + "/cuss/foo"; int cussThreadCount = 2; int cussQueueSize = 100; // for tracking callbacks from CUSS final AtomicInteger successCounter = new AtomicInteger(0); final AtomicInteger errorCounter = new AtomicInteger(0); final StringBuilder errors = new StringBuilder(); @SuppressWarnings("serial") ConcurrentUpdateSolrServer cuss = new ConcurrentUpdateSolrServer(serverUrl, cussQueueSize, cussThreadCount) { @Override public void handleError(Throwable ex) { errorCounter.incrementAndGet(); errors.append(" " + ex); } @Override public void onSuccess(HttpResponse resp) { successCounter.incrementAndGet(); } }; cuss.setParser(new BinaryResponseParser()); cuss.setRequestWriter(new BinaryRequestWriter()); cuss.setPollQueueTime(0); // ensure it doesn't block where there's nothing to do yet cuss.blockUntilFinished(); int poolSize = 5; ExecutorService threadPool = Executors.newFixedThreadPool(poolSize, new SolrjNamedThreadFactory("testCUSS")); int numDocs = 100; int numRunnables = 5; for (int r = 0; r < numRunnables; r++) threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, cuss)); // ensure all docs are sent threadPool.awaitTermination(5, TimeUnit.SECONDS); threadPool.shutdown(); // wait until all requests are processed by CUSS cuss.blockUntilFinished(); cuss.shutdownNow(); assertEquals("post", TestServlet.lastMethod); // expect all requests to be successful int expectedSuccesses = TestServlet.numReqsRcvd.get(); assertTrue(expectedSuccesses > 0); // at least one request must have been sent assertTrue("Expected no errors but got " + errorCounter.get() + ", due to: " + errors.toString(), errorCounter.get() == 0); assertTrue("Expected " + expectedSuccesses + " successes, but got " + successCounter.get(), successCounter.get() == expectedSuccesses); int expectedDocs = numDocs * numRunnables; assertTrue("Expected CUSS to send " + expectedDocs + " but got " + TestServlet.numDocsRcvd.get(), TestServlet.numDocsRcvd.get() == expectedDocs); }
From source file:org.openiot.ide.core.MenuFactory.java
/** * Creates/*from www . j av a 2s .c o m*/ */ private void validateUrls() { // TODO Remove comments for old way // Spawn a new Thread for each url Validation // List<Thread> threads = new ArrayList<>(propertyMap.size()); if (propertyMap.isEmpty()) return; ExecutorService es = Executors.newFixedThreadPool(propertyMap.entrySet().size()); for (Map.Entry<String, HashMap<String, String>> entry : propertyMap.entrySet()) { es.execute(new ValidatorRunnable(entry)); // threads.add(new Thread(new ValidatorRunnable(entry))); } es.shutdown(); try { es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(); } // alternative way to do the above with threads // for (Thread t : threads) { // t.start(); // } // // for (Thread t : threads) { // try { // t.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } }
From source file:ke.co.binary.app.restoppress.restoppress.Service.ImplGenRequests.java
@Override public void sendRequests(int number, String url, Reply json_obj, String method) { long starttime = System.currentTimeMillis(); ExecutorService executor = Executors.newCachedThreadPool(); for (int i = 0; i < number; i++) { //generate random ref code and order info json_obj.getMessage().setReferenceNo(generateRandom()); json_obj.getMessage().setOrderInfo(generateRandom()); //System.out.println("Request Method: "+json_obj.getMessage()); Runnable worker = new ProcessRequest(url, method, json_obj); executor.execute(worker); }/*from w ww. j a va 2 s .c o m*/ executor.shutdown(); //wait for all the threads to terminate while (!executor.isTerminated()) { } long finishtime = System.currentTimeMillis(); time_taken = (finishtime - starttime); }
From source file:com.amour.imagecrawler.ImagesManager.java
/** * Run crawler operation in multi-thread * @param propertiesManager The Properties-Manager * @throws IOException /* www. j ava2 s . c om*/ * @throws java.security.NoSuchAlgorithmException */ public void run(Properties propertiesManager) throws IOException, NoSuchAlgorithmException, Exception { ExecutorService executor = Executors.newFixedThreadPool( Integer.parseInt(propertiesManager.getProperty(Crawler.NUMBER_OF_WORKER_THREADS_KEY))); for (String imageUrl : this.imagesList) { Runnable worker = new ImageRunable(imageUrl, propertiesManager); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } }
From source file:com.shmsoft.dmass.ec2.EC2Agent.java
private void setInitializedState(Cluster cluster) { ExecutorService es = Executors.newCachedThreadPool(); for (Server server : cluster) { LoginChecker checker = new LoginChecker(); checker.setServer(server);/*from w w w.j a v a 2 s .c om*/ server.setCheckerThread(checker); es.execute(checker); } es.shutdown(); boolean finished = false; try { finished = es.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { e.printStackTrace(System.out); } // TODO what to do if 'finished" is false }
From source file:org.commonjava.util.partyline.ConcurrentFirstReaderTest.java
/** * GIVEN:// w w w . j a v a 2 s.c om * <ol> * <li>File exists on disk</li> * <li>File is not opened for reading or writing</li> * <li>The underlying filesystem is slow to respond to lock requests on FileChannel</li> * </ol> * <br/> * WHEN: * <ol> * <li>Two reader threads attempt to open the file at the same time</li> * </ol> * <br/> * THEN: * <ol> * <li>One reader thread should "win" and be the first to open the file</li> * <li>The other reader thread should "join" that first thread's open file</li> * <li>No OverlappingFileLockException should be thrown</li> * </ol> * @throws Exception */ /*@formatter:off*/ @BMRules(rules = { // setup a rendezvous to control thread execution @BMRule(name = "init rendezvous", targetClass = "FileTree", targetMethod = "<init>", targetLocation = "ENTRY", action = "createRendezvous(\"begin\", 2);" + "debug(\"<<<init rendezvous for begin.\")"), // sync up to reduce chances of missing the race condition for the opLock.lock() control. @BMRule(name = "sync FileTree.setOrJoin", targetClass = "FileTree", targetMethod = "setOrJoinFile", targetLocation = "ENTRY", action = "debug(\"Rendezvous read operations.\"); " + "rendezvous(\"begin\"); " + "debug(\"Continue read operations.\");"), // When we try to init a new JoinableFile for INPUT, simulate an IOException from somewhere deeper in the stack. @BMRule(name = "new JoinableFile lock delay", targetClass = "JoinableFile", targetMethod = "<init>", targetLocation = "ENTRY", action = "debug(\"Delaying JoinableFile.init. Lock is: \" + $5); " + "Thread.sleep(500);" + "debug( \"Resuming lock operation.\" );") }) /*@formatter:on*/ @BMUnitConfig(debug = true) @Test public void run() throws Exception { byte[] bytes = new byte[1024 * 1024 * 2]; // let's get some serious data going. Random rand = new Random(); rand.nextBytes(bytes); final ExecutorService execs = Executors.newFixedThreadPool(2); final File f = temp.newFile("child.bin"); FileUtils.writeByteArrayToFile(f, bytes); final CountDownLatch latch = new CountDownLatch(2); final JoinableFileManager manager = new JoinableFileManager(); manager.startReporting(5000, 5000); for (int i = 0; i < 2; i++) { final int k = i; execs.execute(reader(k, manager, f, latch, true)); } latch.await(); }
From source file:com.wavemaker.tools.apidocs.tools.spring.SpringSwaggerParserTest.java
@Test public void testMultiThread2() throws InterruptedException { ExecutorService service = Executors.newFixedThreadPool(4); final Class<VacationController> controllerClass = VacationController.class; for (int i = 0; i < 10; i++) { final int finalI = i; service.execute(new Runnable() { public void run() { Swagger swagger;//w w w .j ava2 s. c om try { swagger = runForSingleClass(controllerClass); } catch (SwaggerParserException e) { throw new RuntimeException("Exception while parsing class:" + controllerClass.getName(), e); } Assert.assertNotNull(swagger); assertEquals(1, swagger.getTags().size()); assertEquals(controllerClass.getName(), swagger.getTags().get(0).getFullyQualifiedName()); try { writeToFile(swagger, "mul_class" + controllerClass.getSimpleName() + "_" + finalI + ".json"); } catch (IOException e) { throw new RuntimeException("Error while writing to file", e); } } }); } service.shutdown(); service.awaitTermination(10, TimeUnit.SECONDS); }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testMultiClientsForServer() throws Exception { // Number of threads final int size = 30; LOG.debug("clientSimple1:" + clientSimple); List<IServiceSimple> serviceSimpleList = new ArrayList<IServiceSimple>(); for (int i = 0; i < size; i++) { IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); LOG.debug("proxyService:" + proxyService); serviceSimpleList.add(proxyService); }//from w ww . j a v a2 s. c o m List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(serviceSimpleList.get(i), i)); } List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); }
From source file:com.test.test.MultipleRequestsForServerTest.java
@Test public void testSingleClientsForServer() throws Exception { // Number of threads final int size = 20; LOG.debug("clientSimple1:" + clientSimple); IServiceSimple proxyService = clientSimple.getProxy(IServiceSimple.class); List<ClientCallableForServer> clientCallableList = new ArrayList<ClientCallableForServer>(); for (int i = 0; i < size; i++) { clientCallableList.add(new ClientCallableForServer(proxyService, i)); }/*from w w w. j a va 2s . co m*/ List<FutureTask<String>> futureTaskList = new ArrayList<FutureTask<String>>(); for (ClientCallableForServer clientCallable : clientCallableList) { futureTaskList.add(new FutureTask<String>(clientCallable)); } long beginTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(futureTaskList.size()); for (FutureTask<String> futureTask : futureTaskList) { executor.execute(futureTask); } boolean ready = false; int[] dones = new int[futureTaskList.size()]; String[] writes = new String[futureTaskList.size()]; int indexValue = 0; while (!ready) { int count = 0; indexValue = 0; for (FutureTask<String> futureTask : futureTaskList) { if (futureTask.isDone() & dones[indexValue] == 0) { writes[indexValue] = futureTask.get(); dones[indexValue] = 1; } indexValue++; } for (int k = 0; k < dones.length; k++) { if (dones[k] == 1) { count++; } } if (count == futureTaskList.size()) { ready = true; } // Thread.sleep(500); } LOG.debug("\n\n\n ====== DONE ====== "); LOG.debug(" time:" + (System.currentTimeMillis() - beginTime) + "ms\n\n"); executor.shutdown(); for (int i = 0; i < writes.length; i++) { LOG.debug("- " + writes[i]); } LOG.debug("\n\n\n ====== DONE ====== \n\n"); Thread.sleep(20000); LOG.debug("\n\n\n\n+++++++++++++++++++++++++"); LOG.debug("New system:"); IServiceSimple proxyService2 = clientSimple.getProxy(IServiceSimple.class); proxyService2.functionNumber1("1", "1"); LOG.debug("\n\n\n\n==========================="); LOG.debug("And just sleep for empty pool"); Thread.sleep(40000); IServiceSimple proxyService3 = clientSimple.getProxy(IServiceSimple.class); proxyService3.functionNumber1("1", "1"); }