Java:当线程池中的所有线程都完成时,通知主类/不同线程中对象的
当ThreadPoolExecutor中的所有线程都完成后,如何通知我的主类实例化ThreadPoolExecutor?
ThreadPoolExecutor threadPool = null; ThreadClass threadclass1; ThreadClass threadclass2; final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(maxPoolSize); puclic MyClass(){ threadPool = new ThreadPoolExecutor(poolSize,maxPoolSize,keepAliveTime,TimeUnit.SECONDS,queue); threadClass1 = new ThreadClass; threadClass2 = new ThreadClass; threadPool.execute(threadClass1); threadPool.execute(threadClass2); //Now I would like to do something until the threadPool is done working //The threads fill a ConcurrentLinkedQueueand I would like to poll //the queue as it gets filled by the threads and output //it to XML via JAX-RS } 编辑1 让我的线程从某个地方获取数据并将这些信息填充到ConcurrentLinkedQueue中我基本上想在MyClass中执行一些操作来用结果更新XML输出.当所有线程都被终止时,我想返回真实的JAX-RS webservice,它实例化了MyClass,因此webservice知道所有数据都已被提取,现在它可以显示最终的XML文件 编辑2 我将队列传递给线程,以便他们可以将项目添加到队列中.当一个驱动程序完成向articleQueue添加项目时,我想在我的主类中执行一个操作,从队列中轮询实体并将其交给响应对象以某种方式显示它. 当我将队列传递给线程时,它们是使用相同的对象还是使用对象的“副本”,以便线程内的更改不会影响主对象?那不是我想要的行为.当我检查Driver中的articleQueue的大小为18时,DriverController中articleQueue的大小为0. 当一个线程向我的while循环之外的队列添加了一些东西时,有没有更好的方法来做出反应?如何修改我的代码以访问不同类中的同一对象? DriverController public class DriverController { Queue<Article> articleQueue; ThreadPoolExecutor threadPool = null; final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>( maxPoolSize); public DriverController(Response response) { articleQueue = new ConcurrentLinkedQueue<Article>(); threadPool = new ThreadPoolExecutor(); Driver driver = new Driver(this.articleQueue); threadPool.execute(driver); // More drivers would be executed here which add to the queue while (threadPool.getActiveCount() > 0) { // this.articleQueue.size() gives back 0 here ... why? if(articleQueue.size()>0){ response.addArticle(articleQueue.poll()); } } } } 司机 public class Driver implements Runnable{ private Queue<Article> articleQueue; public DriverAlliedElectronics(Queue articleQueue) { this.articleQueue = articleQueue; } public boolean getData() { // Here would be the code where the article is created ... this.articleQueue.offer(article); return true; } public void run() { this.getData(); // this.articleQueue.size() gives back 18 here ... } } 解决方法
也许ExecutorCompletionService可能适合您:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html 上面的链接示例: void solve(Executor e,Collection<Callable<Result>> solvers) throws InterruptedException,ExecutionException { CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e); for (Callable<Result> s : solvers) ecs.submit(s); int n = solvers.size(); for (int i = 0; i < n; ++i) { Result r = ecs.take().get(); if (r != null) use(r); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- availableProcessors is already set to [8], rejecting [8
- 在Java中的不同线程中运行后台进程
- 在Java / Android中将参数放在参数之前有什么意义?
- java – 自动装配任务发送到Spring TaskExecutor
- 为什么String方法substring()不在Java中的camelCase中?
- java – 在Windows 7上没有执行Spring计划任务
- java – HashMap只能通过hashCode()[复制]
- android 渐变透明、伸缩、平移、旋转动画效果
- java – Spring验证非空元素的字符串列表
- Lisp是像JVM一样的虚拟机吗?