java - executor service. how to await while all threads will terminate -
please understand concurrency problem.
i have following code thread execution:
public void startpool(final processor processor, configuration config) { executorservice pool = executors.newfixedthreadpool(config.getthreadpoolsize()); (final string source : config.getsourcepaths()) { pool.submit(new runnable() { @override public void run() { processor.process(source); } }); } pool.shutdown(); }
when use 1 thread(config.getthreadpoolsize()
returns 1) expecetd result.
when use thread count - every time different results. in case result - output console. understand if use lot of threads can different order of output different content!!! sometimes, contents absent. looks 1 thread terminated , other died @ moment.
where can search error? @ code or somewhere deeper?
i see situation on picture:
update:
if rewrite code this:
@override public void run() { synchronized ("123") { processor.process(source); } }
it working understand not multithreading)
update
problem threadpool swallows exceptions. resolve problem after catching throwable inside processor.process(source);
, printstacktrace
it seems processor
implementation use not thread-safe: expected output 1 thread not several threads in parallel.
the behavior may unexpected in multi threaded environment.
Comments
Post a Comment