且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java ExecutorService-扩展

更新时间:2023-12-03 13:38:04

我想知道的是,如果我使用10个线程创建了ExcutorService方法,那么invokeAll方法将同时解决10个任务还是一次解决一个任务?

What i want to know is if the method invokeAll, if i created the ExcutorService with 10 threads, will solve 10 tasks at the same time or will solve one at a time?

如果使用十个线程向ExecutorService提交十个任务,它将同时运行所有任务.他们是否可以完全平行且彼此独立进行取决于他们在做什么.但是它们每个都有各自的线程.

If you submit ten tasks to an ExecutorService with ten threads, it will run them all concurrently. Whether they can proceed completely parallel and independent from each-other depends on what they are doing. But they will each have their own thread.

还有另一个问题,如果我说list.get(i).get(),这将在解决后返回PartialSolution吗?

And another question, if i say list.get(i).get() this will return the PartialSolution after it was solved?

是的,它将阻塞直到计算完成(如果尚未完成)并返回其结果.

Yes, it will block until the computation is done (if not done already) and return its result.

我真的不明白,如果我使用2个线程而不是1个线程,为什么时间没有缩短?

I really don't understand why doesn't the time improves if i use 2 threads instead of 1.

我们需要查看更多代码.它们是否在某些共享数据上同步?这些任务需要多长时间?如果它们很短,您可能不会注意到任何区别.如果它们花费的时间更长,请查看JVM线程转储以验证所有这些线程都在运行.

We need to see more code. Do they synchronize on some shared data? How long do these tasks take? If they are very short, you may not notice any difference. If they take longer, look at the JVM thread dump to verify that all of them are running.