List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
From source file:com.laudandjolynn.mytv.proxy.MyTvProxyManager.java
public void prepareProxies(ProxyProvider... providers) { int length = providers == null ? 0 : providers.length; if (length > 0) { int maxThreadNum = Constant.CPU_PROCESSOR_NUM; ThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("MyTv_Find_Proxies_%d") .build();/* w w w.j a va2 s .c o m*/ ExecutorService executorService = Executors .newFixedThreadPool(length > maxThreadNum ? maxThreadNum : length, threadFactory); CompletionService<List<Proxy>> completionService = new ExecutorCompletionService<List<Proxy>>( executorService); providerList.clear(); for (int i = 0; i < length; i++) { final ProxyProvider provider = providers[i]; providerList.add(provider); completionService.submit(new Callable<List<Proxy>>() { @Override public List<Proxy> call() throws Exception { return provider.getProxies(); } }); } executorService.shutdown(); int count = 0; List<Proxy> resultList = new ArrayList<Proxy>(); while (count < length) { try { Future<List<Proxy>> future = completionService.take(); List<Proxy> proxies = future.get(); if (proxies != null) { resultList.addAll(proxies); } } catch (InterruptedException e) { logger.error("get proxies thread has interrupted.", e); } catch (ExecutionException e) { logger.error("get proxies thread has execution fail.", e); } count++; } resultList.add(LOCALHOST_PROXY); PROXY_QUEUE.clear(); PROXY_QUEUE.addAll(resultList); } }
From source file:com.netflix.zeno.diff.TypeDiffOperation.java
@SuppressWarnings("unchecked") public TypeDiff<T> performDiff(DiffSerializationFramework framework, Iterable<T> fromState, Iterable<T> toState, int numThreads) { Map<Object, T> fromStateObjects = new HashMap<Object, T>(); for (T obj : fromState) { fromStateObjects.put(instruction.getKey(obj), obj); }/*from w w w . j ava 2s . co m*/ ArrayList<List<T>> perProcessorWorkList = new ArrayList<List<T>>(numThreads); // each entry is a job for (int i = 0; i < numThreads; ++i) { perProcessorWorkList.add(new ArrayList<T>()); } Map<Object, Object> toStateKeys = new ConcurrentHashMap<Object, Object>(); int toIncrCount = 0; for (T toObject : toState) { perProcessorWorkList.get(toIncrCount % numThreads).add(toObject); toIncrCount++; } ExecutorService executor = Executors.newFixedThreadPool(numThreads, new ThreadFactory() { @Override public Thread newThread(Runnable r) { final Thread thread = new Thread(r, "TypeDiff_" + instruction.getTypeIdentifier()); thread.setDaemon(true); return thread; } }); try { ArrayList<Future<TypeDiff<T>>> workResultList = new ArrayList<Future<TypeDiff<T>>>( perProcessorWorkList.size()); for (final List<T> workList : perProcessorWorkList) { if (workList != null && !workList.isEmpty()) { workResultList.add(executor.submit(new TypeDiffCallable<T>(framework, instruction, fromStateObjects, toStateKeys, workList))); } } TypeDiff<T> mergedDiff = new TypeDiff<T>(instruction.getTypeIdentifier()); for (final Future<TypeDiff<T>> future : workResultList) { try { TypeDiff<T> typeDiff = future.get(); mergeTypeDiff(mergedDiff, typeDiff); } catch (Exception e) { throw new RuntimeException(e); } } for (Map.Entry<Object, T> entry : fromStateObjects.entrySet()) { mergedDiff.incrementFrom(); if (!toStateKeys.containsKey(entry.getKey())) mergedDiff.addExtraInFrom(entry.getValue()); } return mergedDiff; } finally { executor.shutdownNow(); } }
From source file:com.netflix.iep.http.RxHttpTest.java
@BeforeClass public static void startServer() throws Exception { rxHttp.start();// ww w . ja va 2 s .com server = HttpServer.create(new InetSocketAddress(0), 100); server.setExecutor(Executors.newFixedThreadPool(10, new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "HttpServer"); } })); port = server.getAddress().getPort(); server.createContext("/empty", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { ignore(exchange.getRequestBody()); int port = exchange.getRemoteAddress().getPort(); exchange.getResponseHeaders().add("X-Test-Port", "" + port); statusCounts.incrementAndGet(statusCode.get()); exchange.sendResponseHeaders(statusCode.get(), -1L); exchange.close(); } }); server.createContext("/echo", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { Headers headers = exchange.getRequestHeaders(); int contentLength = Integer.parseInt(headers.getFirst("Content-Length")); String contentEnc = headers.getFirst("Content-Encoding"); if (contentEnc != null) { exchange.getResponseHeaders().add("Content-Encoding", contentEnc); } int code = statusCode.get(); if (contentLength > 512 && !"gzip".equals(contentEnc)) { code = 400; } statusCounts.incrementAndGet(code); exchange.sendResponseHeaders(code, contentLength); try (InputStream input = exchange.getRequestBody(); OutputStream output = exchange.getResponseBody()) { byte[] buf = new byte[1024]; int length; while ((length = input.read(buf)) > 0) { output.write(buf, 0, length); } } exchange.close(); } }); server.createContext("/relativeRedirect", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { ignore(exchange.getRequestBody()); if (redirects.get() <= 0) { statusCounts.incrementAndGet(statusCode.get()); exchange.getResponseHeaders().add("Location", "/empty"); exchange.sendResponseHeaders(statusCode.get(), -1L); exchange.close(); } else { redirects.decrementAndGet(); statusCounts.incrementAndGet(302); exchange.getResponseHeaders().add("Location", "/relativeRedirect"); exchange.sendResponseHeaders(302, -1L); exchange.close(); } } }); server.createContext("/absoluteRedirect", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { String host = "http://" + exchange.getRequestHeaders().getFirst("Host"); ignore(exchange.getRequestBody()); if (redirects.get() <= 0) { statusCounts.incrementAndGet(302); exchange.getResponseHeaders().add("Location", host + "/empty"); exchange.sendResponseHeaders(302, -1L); exchange.close(); } else { redirects.decrementAndGet(); statusCounts.incrementAndGet(302); exchange.getResponseHeaders().add("Location", host + "/absoluteRedirect"); exchange.sendResponseHeaders(302, -1L); exchange.close(); } } }); server.createContext("/notModified", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { ignore(exchange.getRequestBody()); statusCounts.incrementAndGet(304); exchange.sendResponseHeaders(304, -1L); exchange.close(); } }); server.createContext("/redirectNoLocation", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { ignore(exchange.getRequestBody()); statusCounts.incrementAndGet(302); exchange.sendResponseHeaders(302, -1L); exchange.close(); } }); server.createContext("/readTimeout", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { ignore(exchange.getRequestBody()); statusCounts.incrementAndGet(statusCode.get()); // So we can track retries Object lock = new Object(); try { synchronized (lock) { lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } }); server.start(); set(client + ".niws.client.MaxAutoRetriesNextServer", "" + retries); set(client + ".niws.client.RetryDelay", "100"); set(client + ".niws.client.ReadTimeout", "1000"); }
From source file:com.tascape.qa.th.SuiteRunner.java
public int startExecution() throws IOException, InterruptedException, SQLException, XMLStreamException { File dir = SYS_CONFIG.getLogPath().resolve(execId).toFile(); LOG.info("Create suite execution log directory {}", dir); if (!dir.exists() && !dir.mkdirs()) { throw new IOException("Cannot create directory " + dir); }//from w w w .ja va 2 s .co m this.logAppProperties(dir); int threadCount = SYS_CONFIG.getExecutionThreadCount(); LOG.info("Start execution engine with {} thread(s)", threadCount); int len = (threadCount + "").length(); ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("t%0" + len + "d").build(); ExecutorService executorService = Executors.newFixedThreadPool(threadCount, namedThreadFactory); CompletionService<TestResult> completionService = new ExecutorCompletionService<>(executorService); LOG.info("Start to acquire test cases to execute"); int numberOfFailures = 0; try { List<TestResult> tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100)); while (!tcrs.isEmpty()) { List<Future<TestResult>> futures = new ArrayList<>(); for (TestResult tcr : tcrs) { LOG.info("Submit test case {}", tcr.getTestCase().format()); futures.add(completionService.submit(new TestRunnerJUnit4(db, tcr))); } LOG.debug("Total {} test cases submitted", futures.size()); for (Future<TestResult> f : futures) { try { Future<TestResult> future = completionService.take(); TestResult tcr = future.get(); if (tcr == null) { continue; } String result = tcr.getResult().result(); LOG.info("Get result of test case {} - {}", tcr.getTestCase().format(), result); if (!ExecutionResult.PASS.name().equals(result) && !result.endsWith("/0")) { numberOfFailures++; } } catch (Throwable ex) { LOG.error("Error executing test thread", ex); numberOfFailures++; } } tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100)); } } finally { AbstractSuite.getSuites().stream().forEach((suite) -> { try { suite.tearDown(); } catch (Exception ex) { LOG.warn("Error tearing down suite {} - {}", suite.getClass(), ex.getMessage()); } }); } executorService.shutdown(); LOG.info("No more test case to run on this host, updating suite execution result"); this.db.updateSuiteExecutionResult(this.execId); this.db.saveJunitXml(this.execId); return numberOfFailures; }
From source file:com.chicm.cmraft.core.NodeConnectionManager.java
public void appendEntries(RaftLog logMgr, long lastApplied) { int nServers = getRemoteServers().size(); if (nServers <= 0) { return;//w w w.j a v a 2 s.com } ExecutorService executor = Executors.newFixedThreadPool(nServers, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName(getRaftNode().getName() + "-AsyncRpcCaller" + (byte) System.currentTimeMillis()); return t; } }); for (ServerInfo server : getRemoteServers()) { NodeConnection conn = connections.get(server); long startIndex = logMgr.getFollowerMatchIndex(server) + 1; LOG.info(getRaftNode().getName() + ": SENDING appendEntries Request TO: " + server); Thread t = new Thread(new AsynchronousAppendEntriesWorker(getRaftNode(), conn, getRaftNode().getRaftLog(), getRaftNode().getServerInfo(), getRaftNode().getCurrentTerm(), logMgr.getCommitIndex(), startIndex - 1, logMgr.getLogTerm(startIndex - 1), logMgr.getLogEntries(startIndex, lastApplied), lastApplied)); t.setDaemon(true); executor.execute(t); } }
From source file:io.stallion.jobs.JobCoordinator.java
private JobCoordinator() { queue = new PriorityBlockingQueue<>(); BasicThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern("stallion-job-execution-thread-%d").build(); // Create an executor service for single-threaded execution pool = Executors.newFixedThreadPool(25, factory); registeredJobs = new HashSet<>(); }
From source file:com.clustercontrol.selfcheck.SelfCheckTaskSubmitter.java
public SelfCheckTaskSubmitter() { _scheduler = Executors.newScheduledThreadPool(1, new ThreadFactory() { @Override// www . j ava2 s .co m public Thread newThread(Runnable r) { return new Thread(r, "SelfCheckScheduler"); } }); _executorService = Executors.newFixedThreadPool( HinemosPropertyUtil.getHinemosPropertyNum("selfcheck.threadpool.size", Long.valueOf(4)).intValue(), new ThreadFactory() { private volatile int _count = 0; @Override public Thread newThread(Runnable r) { return new Thread(r, "SelfCheckWorker-" + _count++); } }); }
From source file:koper.client.DefaultConsumerLauncher.java
private void startMessageReceiver(int size) { ExecutorService receiverPool = Executors.newFixedThreadPool(size, new NamedThreadFactory(MessageReceiverThread.class.getSimpleName())); try {//from w w w . ja v a2 s. co m for (int i = 0; i < size; i++) { MessageReceiver messageReceiver = this.messageReceiverClass.newInstance(); Runnable consumerThread = new MessageReceiverThread(properties, messageReceiver, messageCenter, this.listenerRegistry, this.partitions); receiverPool.execute(consumerThread); } } catch (Exception e) { log.error("startMessageReceiver :{}", e); throw new RuntimeException(e); } }
From source file:com.reactive.hzdfs.core.DistributedFileSupportService.java
@PostConstruct private void init() { commandTopicId = hzService.addMessageChannel(this); threads = Executors.newFixedThreadPool(nThreads, new ThreadFactory() { int n = 0; @Override//w ww .jav a2s . c om public Thread newThread(Runnable r) { Thread t = new Thread(r, "DFSS-Worker-" + (n++)); return t; } }); recordMapCfg = RecordMapConfig.class.getAnnotation(IMapConfig.class); log.info("[DFSS] File distribution service initialized"); }
From source file:com.haulmont.cuba.core.app.scheduling.RunnerBean.java
@PostConstruct public void init() { int nThreads = configuration.getConfig(ServerConfig.class).getSchedulingThreadPoolSize(); executorService = Executors.newFixedThreadPool(nThreads, new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(1); @Override// ww w . j a v a2 s .co m public Thread newThread(@Nonnull Runnable r) { Thread thread = new Thread(r, "ScheduledRunnerThread-" + threadNumber.getAndIncrement()); thread.setDaemon(true); return thread; } }); }