1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class ThreadUtils {
public static ExecutorService newFixedThreadPool() { return new ThreadPoolExecutor(4,4, 10L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(100), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); }
public static <T> void executerPool(List<T> list) throws InterruptedException { ExecutorService executorService = newFixedThreadPool(); List<T> collect = list.stream().filter(l -> l instanceof Runnable).collect(Collectors.toList()); final CountDownLatch count=new CountDownLatch(collect.size()); for (T runnable : collect) { Customers customers = (Customers) runnable; customers.counts(count); executorService.execute(customers); } count.await(); executorService.shutdown(); }
public static <T> void executerPool2(List<T> list) throws InterruptedException { ExecutorService executorService = newFixedThreadPool(); List<Customers> runnables = new ArrayList<>(); for (T t : list) { if(t instanceof Customers){ runnables.add((Customers) t); } } final CountDownLatch count=new CountDownLatch(runnables.size()); for (Customers runnable : runnables) { runnable.counts(count); executorService.execute(runnable); } count.await(); executorService.shutdown(); }
}
|