Java中的并行处理;需要的建议,即在Runnanble / Callable接口 [英] Parallel-processing in Java; advice needed i.e. on Runnanble/Callable interfaces

查看:210
本文介绍了Java中的并行处理;需要的建议,即在Runnanble / Callable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一组需要以两种不同方式分析的对象,这两种方式都需要相对长的时间并涉及IO调用,我试图找出如何/如果我可以去优化这个部分的我的软件,特别是利用多个处理器(我正在坐的ex是一个8核i7,在执行过程中几乎不会超过10%的负载)。



我对并行编程或多线程(不知道什么是正确的术语)是新的,所以我已经阅读了一些先前的问题,特别是注意高度投票和信息的答案。我还正在浏览 Oracle / Sun并发性教程



这是我到目前为止所想的;




  • 线程安全集合保存要分析的对象


  • 每个特定的线程负责初始的预分析准备工作;在每个对象的线程中,



这两个分析是作为Runnables / Callables实现的,因此在需要时由线程调用。

我的问题是:


  1. 这是一个合理的计划,如果没有,去做这个?

  2. 为了确保事情不会失控,我应该实现一个ThreadManager或某种排序的东西,启动和停止线程,并重新分配它们,当他们是否完整?例如,如果我有256个待分析的对象,总共有16个线程,则ThreadManager将第一个完成的线程分配给第17个要分析的对象等。

  3. 是否有显着的差异之间Runnable / Callable除了Callable可以返回一个结果的事实?否则应该尝试实现我自己的接口,在这种情况下为什么?

感谢,

解决方案


  1. 您可以使用 BlockingQueue 实现来保存你的对象,并从那里产生你的线程。此接口基于 producer-consumer 原则。 put( )方法将阻塞如果您的队列已满,直到有更多的空间,并且 take()方法将阻塞队列是否为空,直到队列中再次有一些对象。



  2. 如果您正在等待来自您生成的主题的结果,那么 Callable 接口是一个好主意,因为您可以提前开始计算,并在代码中使用未来 -s中的结果。至于与 Runnable 接口的区别,请从 Callable javadoc



    Callable接口类似于Runnable,其实例可能由另一个线程执行。


一些常规的在您寻求java并发时需要考虑的事情:




  • volatile AtomicReference java.util.concurrent.atomic 包中的其他对象是您的朋友。

  • 使用同步和锁定的复合动作的原子性


Assume that I have a set of objects that need to be analyzed in two different ways, both of which take relatively long time and involve IO-calls, I am trying to figure out how/if I could go about optimizing this part of my software, especially utilizing the multiple processors (the machine i am sitting on for ex is a 8-core i7 which almost never goes above 10% load during execution).

I am quite new to parallel-programming or multi-threading (not sure what the right term is), so I have read some of the prior questions, particularly paying attention to highly voted and informative answers. I am also in the process of going through the Oracle/Sun tutorial on concurrency.

Here's what I thought out so far;

  • A thread-safe collection holds the objects to be analyzed
  • As soon as there are objects in the collection (they come a couple at a time from a series of queries), a thread per object is started
  • Each specific thread takes care of the initial pre-analysis preparations; and then calls on the analyses.
  • The two analyses are implemented as Runnables/Callables, and thus called on by the thread when necessary.

And my questions are:

  1. Is this a reasonable scheme, if not, how would you go about doing this?
  2. In order to make sure things don't get out of hand, should I implement a ThreadManager or some thing of that sort, which starts and stops threads, and re-distributes them when they are complete? For example, if i have 256 objects to be analyzed, and 16 threads in total, the ThreadManager assigns the first finished thread to the 17th object to be analyzed etc.
  3. Is there a dramatic difference between Runnable/Callable other than the fact that Callable can return a result? Otherwise should I try to implement my own interface, in that case why?

Thanks,

解决方案

  1. You could use a BlockingQueue implementation to hold your objects and spawn your threads from there. This interface is based on the producer-consumer principle. The put() method will block if your queue is full until there is some more space and the take() method will block if the queue is empty until there are some objects again in the queue.

  2. An ExecutorService can help you manage your pool of threads.

  3. If you are awaiting a result from your spawned threads then Callable interface is a good idea to use since you can start the computation earlier and work in your code assuming the results in Future-s. As far as the differencies with the Runnable interface, from the Callable javadoc:

    The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

Some general things you need to consider in your quest for java concurrency:

  • Visibility is not coming by defacto. volatile, AtomicReference and other objects in the java.util.concurrent.atomic package are your friends.
  • You need to carefully ensure atomicity of compound actions using synchronization and locks.

这篇关于Java中的并行处理;需要的建议,即在Runnanble / Callable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆