I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } //...wait for completion somehow How can I get notified once all of them are complete? For now I can't think about anything better than setting some global task counter and decrease it at the end of e
我需要一次执行一些任务4,如下所示: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } //...wait for completion somehow 一旦所有人都完成了,我怎样才能得到通知? 现在我无法想象设置一些全局任务计数器并在每个任务结束时减少它,然后在无限循环中监视此计数器变为0; 或者得到一份期货清单,并在无限循环监控isDone的所有人。 什么是不涉及
什么是使用的优势ExecutorService在正在运行的线程传递一个Runnable到Thread构造函数? ExecutorService abstracts away many of the complexities associated with the lower-level abstractions like raw Thread . It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed as Runnable or Callable ). From JCiP, Sec
什么是使用的优势ExecutorService在正在运行的线程传递一个Runnable到Thread构造函数? ExecutorService抽象出许多与较低级别抽象(如原始Thread相关的复杂性。 它为成功或突然终止任务(表示为Runnable或Callable )提供了安全启动,关闭,提交,执行和阻止的机制。 从JCiP,第6.2节,直接从马的嘴巴: Executor可能是一个简单的接口,但它构成了异步任务执行的灵活和强大框架的基础,该框架支持各种任务执行策略。 它提
I just finished reading this post: What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool? and felt that the answer is not straight enough. Can you explain in simple language and examples, what are the trade-offs between Java 7's Fork-Join framework and the older solutions? I also read the Google's #1 hit on the topic Java Tip: When to use ForkJoinPool vs
我刚读完这篇文章:Java-5 ThreadPoolExecutor比Java-7 ForkJoinPool有什么优势? 并觉得答案不够直。 你能用简单的语言和例子来解释一下,Java 7的Fork-Join框架和旧的解决方案之间有什么权衡 ? 我也看到了谷歌的#1命中的话题Java技巧:当使用ForkJoinPool VS的ExecutorService从javaworld.com但是, 当它谈论大多API差异的文章并没有回答标题问题... 分叉连接允许您轻松执行分割和征服作业,如果要在ExecutorService
I want to achieve the following: When my application starts, the main thread will start 1+ worker threads that should run in the background, and periodically do things behind the scenes. These should not block the main thread: once main starts the workers, it continues doing its own thing until: The main thread finishes (normal application termination) - in the case of a command-line utility t
我想要实现以下功能:当我的应用程序启动时,主线程将启动1+个应该在后台运行的工作线程,并定期在幕后做些事情。 这些不应该阻止主线:一旦主要启动工人,它会继续做自己的事情,直到: 主线程完成(正常应用程序终止) - 在命令行工具的情况下,这是当main(String[])方法的末尾到达时; 在Swing GUI的情况下,当用户选择File >> Exit菜单等时 操作系统会抛出kill命令(SIGKILL等) 在主线程中出现意外的未捕获的
Currently, I'm running on a thread-less model that isn't working simply because I'm running out of memory before I can process the data I'm being handed. I've made all the changes that I can to optimize the code, and it's still just not quite quick enough. Clearly I should move on to a threaded model. I'm wondering what the simplest, easiest way to do the following
目前,我正在运行一个无线程模型,该模型无法正常工作,因为我在处理交付的数据之前内存不足。 我已经做了所有可以优化代码的更改,但仍然不够快。 显然我应该转向线程模型。 我想知道做到以下几点的最简单,最简单的方法是: 主线程将一些信息传递给工作人员 该工作人员执行一些我将重构主要方法的工作 工作人员将消失,并在需要时实例化新工作人员 我从来没有使用过Java线程,从我读过的东西看起来相当复杂,即使
I have a long running process that listens to events and do some intense processing. Currently I use Executors.newFixedThreadPool(x) to throttle the number of jobs that runs concurrently, but depending of the time of the day, and other various factors, I would like to be able to dynamically increase or decrease the number of concurrent threads. If I decrease the number of concurrent threads,
我有一个长时间运行的过程,可以监听事件并进行一些紧张的处理。 目前我使用Executors.newFixedThreadPool(x)来限制并发运行的作业数量,但取决于一天的时间和其他各种因素,我希望能够动态增加或减少并发线程数。 如果我减少并发线程的数量,我希望当前正在运行的作业能很好地完成。 是否有一个Java库让我控制并动态增加或减少线程池中运行的并发线程数量? (该类必须实现ExecutorService)。 我必须自己执行它吗?
I'm trying to implement multi-threading for some task parallelism in a program i am writing. The program is utilizes Spring framework and runs on Pivotal Cloud Foundry. It was crashing occasionally so i went in and viewed the logs and performance metrics; which is when i discovered it has a memory leak. after some testing i narrowed down the culprit to my threading implementation. My und
我试图在我正在编写的程序中为某些任务并行性实现多线程。 该计划使用Spring框架并在Pivotal Cloud Foundry上运行。 它偶尔会崩溃,所以我进去查看了日志和性能指标; 这是当我发现它有内存泄漏。 在进行一些测试后,我将线人的实施范围缩小到了罪魁祸首。 我对JVM中的GC的理解是,它不会处理未死的线程,也不会处理任何仍在被另一个对象或后面的可执行代码行引用的对象。 然而,我并没有对线程进行任何引用,如果我这样做
The same question has been answered again (Creating a memory leak with Java) but now that Java 8 removed the Permanent Generation I think that it would be useful to update this topic since most of the answers depend on exhausting the free space of the Permanent Generation. So, what is the easiest way to create a memory leak in Java 8? I think a common inner class memory leak would work. Exam
同样的问题已经得到了回答(用Java创建内存泄漏),但现在Java 8删除了永久代。我认为更新这个主题将是有用的,因为大多数答案都依赖于耗尽永久代的空闲空间。 那么,在Java 8中创建内存泄漏的最简单方法是什么? 我认为一个普通的内部类内存泄漏会起作用。 取自互联网的示例: public class LeakFactory {//Just so that we have some data to leak int myID = 0; // Necessary because our Leak class is non-static
I am utilizing ImageIO.read(). The class which is called by the main method of the original App is this: import java.awt.*; import javax.swing.*; import java.io.File; import java.awt.image.BufferedImage; import java.awt.event.*; import javax.swing.JPanel; class ImageGenerator extends JPanel{ JpegReader jpeg; public ImageGenerator(Aplicacion a){ jpeg = new JpegReader(); loadImage();
我正在使用ImageIO.read()。 被原始应用程序的主要方法调用的类是这样的: import java.awt.*; import javax.swing.*; import java.io.File; import java.awt.image.BufferedImage; import java.awt.event.*; import javax.swing.JPanel; class ImageGenerator extends JPanel{ JpegReader jpeg; public ImageGenerator(Aplicacion a){ jpeg = new JpegReader(); loadImage(); } private void loadImage(){
This question already has an answer here: Java inner class and static nested class 23 answers When I read that book, I took that paragraph this way: If a class is used by another class and there is no other way it would be used by some other class by any means, move it to the class that uses it as an inner class. This usage renders the inner class as a "private" feature of encaps
这个问题在这里已经有了答案: Java内部类和静态嵌套类23答案 当我读到那本书时,我这样说了那段话: 如果某个类被另一个类使用,并且没有其他方式可以被其他类使用,则将其移动到将其用作内部类的类。 这种用法将内部类作为封装类的“私有”特性。 因此,为了保持整洁,有点类似封装,作者建议采取行动。 从这个角度来看,这对我有意义。 简而言之, 封装在任何有意义的地方,但不要强制它,如果它没有。