How to print out contents to a file from two or more threads for two sections?

I have a thread implementing the run method. I have two entities payrollDept and hrDept with a superclass Department.

I have another class which passes in the startCounter and endCounter for each thread
Class TestRunnerProcessor{

//processing 100 objects with 6 threads as an example
// I divide the 100 payroll and hrdept objects by 6 threads
//creating each runner object with a start counter and end counter here

writer.writeHeader("Creating report for failures with payroll and Hrdept");
TestRunner(int startC, int endC, List <payrollDept> lst1, List<HrDept> lst2);;
writer.writeFooter(" Completed payroll and Hrdept");

Class TestRunner{

TestRunner (int startCounter, int endCounter, List<payrollDept> lst1, List<Hrdept> lst2)

Void run(){

For (int i=startCounter; i<=endCounter;i++)
processListAttribute(Department, writer)

processListAttribute(Department dpt, writer wt)
// perform some business logic here

//on failure


My question is that I want to write failures with payrollDept and with HrDept to the file. I also want to create a header each for payrollDept  and HrDept in the report file. What would be the best way to design a solution for this problem?

Who is Participating?
I think you can do either way either combining your dpertements into lists or if your output is not that complex
and you can write csv or html manually, jurts combining output to strings.
at the very beggining maybe before you split into threaeds:
String s_hr_out = "";
String s_payroll_out = "";
s_hr_out += hedaer(HrDept);
s_payroll += header(payarollDept);

then say in one thread (if you do each type in each thread):
s_hr_out += report(HrDpt);

in another thread

s_payrol_out += report(PayrollDept):

when bothe threads are finihsed

you write into the file first string
and then string

pkrish80Author Commented:
The problem I see is that each thread can execute at any given time and will write payrollDept type failures and HrDept type failures to the report at any given time. I would like to have a header for each section printed in my report. The payrollDept and HrDept have a superclass, Department and I will be passing in references of Department in my processListAttribute method.

Is there any way I can order the processing of the list efficiently?
Combine printout form ieach theread into a string (seperate string for each thread obviously)
Wheh the output is ready then printe each of the strings to separate file or to the sane file - first one struingk then another string
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Or separate string for each type of departemnnts - didn't undesratdn exactly your situation, but you can do it either way when accumulating the output to strings fisrt and then pritout the whole thing when it is ready
pkrish80Author Commented:
I didn't understand your comments. The file will be a csv file or html file. The writer object uses csvPrinter(apache commons) as the underlying printer object. Should I combine the type of department failures in a lis, which is a synchronized or array blocking queue? Can you give an example or pseudo-code?
If in each of the threads you have a mix of departments
then use this:
if(dpt instance of HRDpt) write to one string
else if(dpt instance of PayrollDpt) write to another string

Still in the end forst printout first string then secion string
I think String in java can be as long as overall memory allows so you should not have a problem from this side
pkrish80Author Commented:
the solution isn't what I looking for. Maybe the question can be framed better.
pkrish80Author Commented:
I followed a different solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.