M.J.
M.J.

Reputation: 16666

multiple threads injecting data into the subsequent process

I have a project made using Java.

I have a complex processing, something like from one single process i create 10 different threads, then the process waits for the other threads to complete processing. Now the threads that were created do some database processsing, and then finally generates the output. But the problem here is, the process that have been waiting, again needs to process all the data that was created in the threads that were created, sort of aggregated result.

I am almost clueless what needs to be done.

Regards

Upvotes: 2

Views: 209

Answers (6)

Scorpion
Scorpion

Reputation: 3986

You can use java.util.concurrent.CompletionService to submit and poll for the task completion.

Alternatively look into CountdownLatch or the CyclicBarrier classes.

Let me know if you need examples because I assume internet would already be flooded with such examples; also the javadocs are pretty good and it is always a good learning curve to do it first hand.

Upvotes: 0

Matjaz Muhic
Matjaz Muhic

Reputation: 5598

You could just use a shared object to add data to it.

If I understand right then:

Create a class that will hold all data in the end (for example MyData). This class could have "getData" method that will return data and "add" method which will add data to some collection of your choice (array, list, ...).

Then when a thread is done with processing the data it calls:

MyData.add(partialDataFromThread)

And in the end your main class will do:

MainClass.process(MyData.getDatA());

Hope it helps...

Upvotes: 0

Bhaskar
Bhaskar

Reputation: 7523

If your difficulty is how to wait in the main thread until child threads complete their work , then you can use childThread.join() on child threads from the main thread. If you are troubled by how to make the results brought by the child threads from db availble to the main thread for processing , then use some shared data structure which is populated by the child threads and which is then accessed by the main thread. ( Make sure you synchronize properly )

For all such tasks however , it is best to use Executor framework in Java 1.6.

Upvotes: 0

mamboking
mamboking

Reputation: 4637

You could use a java.util.concurrent.ConcurrentLinkedQueue. Have each thread put their results on the queue when they're done. The main thread just watches the queue and processes the results as they come in.

Another alternative is to use Futures. Instead of threads just use Futures for each of the processes. The main thread will block while waiting for each future to finish it's processing.

Upvotes: 3

Jean Logeart
Jean Logeart

Reputation: 53839

You might consider using a BlockingQueue to aggregate all your data in one data structure.

This queue can then be used by your main process (even before all your threads actually finished their work).

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692081

You'll need to start 10 threads in your main thread, and wait for them to finish. This can be done calling Thread.join() on each of the 10 started threads (after they are all started).

For more information about threads, read the Java tutorial about concurrency.

Upvotes: 0

Related Questions