multithreading - Parallel-processing in Java; advice needed i.e. on Runnanble/Callable interfaces -


assume have set of objects need analyzed in 2 different ways, both of take relatively long time , involve io-calls, trying figure out how/if go optimizing part of software, utilizing multiple processors (the machine sitting on ex 8-core i7 never goes above 10% load during execution).

i quite new parallel-programming or multi-threading (not sure right term is), have read of prior questions, particularly paying attention highly voted , informative answers. in process of going through oracle/sun tutorial on concurrency.

here's thought out far;

  • a thread-safe collection holds objects analyzed
  • as there objects in collection (they come couple @ time series of queries), thread per object started
  • each specific thread takes care of initial pre-analysis preparations; , calls on analyses.
  • the 2 analyses implemented runnables/callables, , called on thread when necessary.

and questions are:

  1. is reasonable scheme, if not, how go doing this?
  2. in order make sure things don't out of hand, should implement threadmanager or thing of sort, starts , stops threads, , re-distributes them when complete? example, if have 256 objects analyzed, , 16 threads in total, threadmanager assigns first finished thread 17th object analyzed etc.
  3. is there dramatic difference between runnable/callable other fact callable can return result? otherwise should try implement own interface, in case why?

thanks,

  1. you use blockingqueue implementation hold objects , spawn threads there. interface based on producer-consumer principle. put() method block if queue full until there more space , take() method block if queue empty until there objects again in queue.

  2. an executorservice can manage pool of threads.

  3. if awaiting result spawned threads callable interface idea use since can start computation earlier , work in code assuming results in future-s. far differencies runnable interface, callable javadoc:

    the callable interface similar runnable, in both designed classes instances potentially executed thread. runnable, however, not return result , cannot throw checked exception.

some general things need consider in quest java concurrency:

  • visibility not coming defacto. volatile, atomicreference , other objects in java.util.concurrent.atomic package friends.
  • you need ensure atomicity of compound actions using synchronization , locks.

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -