Tag: executorservice

动态线程池

我有一个长时间运行的过程,它监听事件并进行一些密集的处理。 目前我使用Executors.newFixedThreadPool(x)来限制并发运行的作业数,但是根据一天中的时间和其他各种因素,我希望能够动态增加或减少并发线程数。 如果我减少并发线程的数量,我希望当前正在运行的作业能够很好地完成。 是否有一个Java库可以让我控制并动态增加或减少在线程池中运行的并发线程数? (该类必须实现ExecutorService)。 我必须自己实施吗?

ExecutorService与Casual Thread Spawner

我有一个关于ExecutorService如何在Java中工作的基本问题。 很难看到简单地创建Threads以并行执行某些任务和将每个任务分配给ThreadPool之间的区别。 ExecutorService看起来也非常简单有效,所以我想知道为什么我们不会一直使用它。 这只是一种比另一种方式更快地执行其工作的问题吗? 这里有两个非常简单的例子来说明两种方式之间的区别: 使用执行程序服务:Hello World(任务) static class HelloTask implements Runnable { String msg; public HelloTask(String msg) { this.msg = msg; } public void run() { long id = Thread.currentThread().getId(); System.out.println(msg + ” from thread:” + id); } } 使用执行程序服务:Hello World(创建执行程序,提交) static class HelloTask { public static void main(String[] args) { int ntasks = […]

如何在java执行器类中停止所有可运行的线程?

final ExecutorService executor = Executors.newFixedThreadPool(1); final Future future = executor.submit(myRunnable); executor.shutdown(); if(executor.awaitTermination(10, TimeUnit.SECONDS)) { System.out.println(“task completed”); }else{ System.out.println(“Executor is shutdown now”); } //MyRunnable method is defined as task which I want to execute in a different thread. 这是run者类的run方法: public void run() { try { Thread.sleep(20 * 1000); } catch (InterruptedException e) { // TODO Auto-generated […]

Java执行官:如何停止提交的任务?

我已经使用执行程序提交了一个任务,我需要它在一段时间后停止(例如5分钟)。 我试过这样做: for (Future fut : e.invokeAll(tasks, 300, TimeUnit.SECONDS)) { try { fut.get(); } catch (CancellationException ex) { fut.cancel(true); tasks.clear(); } catch(ExecutionException ex){ ex.printStackTrace(); //FIXME: gestita con printstack } } 但我总是得到一个错误:我有一个需要被任务修改然后由线程读取的共享Vector,即使我停止所有任务,如果发生超时,我得到: Exception in thread “Thread-1” java.util.ConcurrentModificationException 有什么不对? 如何停止提交的5分钟后仍在工作的任务?

如何使用invokeAll()让所有线程池完成他们的任务?

ExecutorService pool=Executors.newFixedThreadPool(7); List<Future> future=new ArrayList<Future>(); List<Callable> callList = new ArrayList<Callable>(); for(int i=0;i<=diff;i++){ String str="2013-"+(liDates.get(i).get(Calendar.MONTH)+1)+"-"+liDates.get(i).get(Calendar.DATE); callList.add(new HotelCheapestFare(str)); } future=pool.invokeAll(callList); for(int i=0;i<=future.size();i++){ System.out.println("name is:"+future.get(i).get().getName()); } 现在我希望pool在进入for循环之前invokeAll所有的任务但是当我运行这个程序for循环时,在invokeAll之前执行并抛出此exception: java.util.concurrent.ExecutionException: java.lang.NullPointerException at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source) at java.util.concurrent.FutureTask.get(Unknown Source) at com.mmt.freedom.cheapestfare.TestHotel.main(TestHotel.java:6‌​5) Caused by: java.lang.NullPointerException at com.mmt.freedom.cheapestfare.HotelCheapestFare.getHotelCheap‌estFare(HotelCheapes‌​tFare.java:166) at com.mmt.freedom.cheapestfare.HotelCheapestFare.call(HotelChe‌​apestFare.java:219) at com.mmt.freedom.cheapestfare.HotelCheapestFare.call(HotelChe‌​apestFare.java:1) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) atjava.util.concurrent.ThreadPoolExecutor$Worker.run(Unknow‌​n Source) at […]

如何正确使用Java Executor?

我在我的multithreading应用程序中使用过Java Executors,但我似乎无法弄清楚何时最好使用以下各种方法: 1。 ExecutorService executor=Executors.newFixedThreadPool(50); executor.execute(new A_Runner(… some parameter …)); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 2。 int Page_Count=200; ExecutorService executor=Executors.newFixedThreadPool(50); doneSignal=new CountDownLatch(Page_Count); for (int i=0;i<Page_Count;i++) executor.execute(new A_Runner(doneSignal, … some parameter …)); doneSignal.await(); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 3。 int Executor_Count=30; ThreadPoolExecutor executor=new ThreadPoolExecutor(Executor_Count,Executor_Count*2,1,TimeUnit.SECONDS,new LinkedBlockingQueue()); List<Future> futures=new ArrayList(3330); for (int i=0;i<50;i++) futures.add(executor.submit(new A_Runner(… some […]