• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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);
TestRunner.run();
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
wt.addFailure(dpt, dpt.name);


}

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?

Thanks
0
pkrish80
Asked:
pkrish80
  • 5
  • 4
1 Solution
 
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?
0
 
for_yanCommented:
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
0
 
for_yanCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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?
0
 
for_yanCommented:
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
s_hr_out
and then string
s_pyarol_out









0
 
for_yanCommented:
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
0
 
for_yanCommented:
I think String in java can be as long as overall memory allows so you should not have a problem from this side
0
 
pkrish80Author Commented:
the solution isn't what I looking for. Maybe the question can be framed better.
0
 
pkrish80Author Commented:
I followed a different solution.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now