The moose likes Threads and Synchronization and the fly likes Batch job with multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Batch job with multiple threads" Watch "Batch job with multiple threads" New topic

Batch job with multiple threads

Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 167
I have a batch job which has 2 steps, Step A and Step B. Step B should always execute after A is complete. Step A involves processing many files, the names of which we get as a list. Now I have been asked to divide step A into multiple threads, each of which will execute a finite number of files. If I start multiple threads, how do I ensure that Step B starts only after all threads processing step A have completed?



If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19739

Either as part of step A, or immediately after it, you wait for all those threads to be completed. Then you start on step B.
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 167
Thank you for the response.
Based on your suggestion, here is what I pan to do:

Suppose there are 10n files to be processed, and I decide to allocate 10 files per thread, so I will create 'n' threads and add to a collection.
Then I keep checking each thread's state to see if the execution is complete( using getState()), if so, start Process B. Else wait.

Does the above seem good enough? You do you have a better approach, about which you can give some hints?

Thanks once again

Martin Vajsar

Joined: Aug 22, 2010
Posts: 3719

You would just invoke the join() method on all threads in the collections. Join waits until the "target" thread completes, so you'd know that after invoking join on every thread, process A is done. And, unlike periodic checking of individual thread states, this waiting is done efficiently.

I'd strongly suggest using some of the library functions (Executors) for this task, though. Have a look at this tutorial.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Also, since you will undoubtedly be sending those file from one thread to another, you should look into either a SynchronousQueue (one directional information dump) or Exchanger (two directional information exchange), which could help both the sequencing and the exchange of data.

Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 167
Thanks for the information. I am currently going through the tutorial. Also, agree that join works better than my approach.

@Steve: The directive given to me was not to make the threads I create for Step A to be synchronous. Step A and Step B should be synched, with Step A being done by multiple threads. However, I will surely keep your input in mind if I need to build a synchronous activity.

Thanks to all
I agree. Here's the link:
subject: Batch job with multiple threads
It's not a secret anymore!