This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i convert this code to use threads?

 
bob connolly
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !

I have a loop that goes thru an array of items and if an item was selected it submits a program, but some of the programs take days to run, so any other jobs in the selected list have to wait for the long running job to finish!

Is there a way i can set up these job classes to run independently, i'm guessing using threads, but not sure if i can now convert this approach to a threaded approach, please see where the programs are instantiated, see "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"

Thanks very much
bc

[code}

for (int i=0;i<r1.io.length;i++)
{
err1=false; err2=false;

String sel = new String();
if (r1.io[i][1]==null) {} else {sel=(r1.io[i][1]).toString();}

if (sel.equals("true"))
{
String s = new String();
s=(r1.io[i][6]).toString();
if (r1.DEBUG) System.out.println("selected: "+s);

XlsParms xp = r1.createDoc(i);

if (xp.val_type.equals("HVOBX"))
{
if (r1.DEBUG) System.out.println("in HVOBX in HlpvalXls1");
try
{
try
{
FileOutputStream fos = new FileOutputStream(xp.rep_file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
if (!xp.XsdFilIsGood) r1.xsdInvalid(i,xp.xsd_file,"");
if (!xp.OraQryIsGood) r1.qryInvalid(i,r1.bl_file_tbl,"");
System.out.println("Submitting the "+xp.val_type+" validation process-> report
if (BKGRND_ON) System.setOut(ps);

HlpValOraBufXml1 h = new HlpValOraBufXml1(xp); <<<<<<<<<<<<<<<<<<<<<<<<<<<

if (BKGRND_ON) System.setOut(orig);
int l = r1.getLineCnt(xp.rep_file);
if (l>8) {
String e=new String("Error -> See report in file:
System.out.println(e);
String sio=r1.io[i][3].toString();sio=sio.concat(e);r1.io[i][3]=new String(sio).trim();
}
else { r1.io[i][3]=new String(" ");}
} catch (FileNotFoundException fnfe){System.out.println("rep_file-FileNotFound:"+fnfe);}
} catch (Exception e) {System.out.println(e);}
}

if (r1.DEBUG) System.out.println("xp.val_type: "+xp.val_type);

if (xp.val_type.equals("HVOEX"))
{
if (r1.DEBUG) System.out.println("in HVOEX in HlpvalXls1");
try
{
try
{
FileOutputStream fos = new FileOutputStream(xp.rep_file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
if (!xp.XsdFilIsGood) r1.xsdInvalid(i,xp.xsd_file,"");
if (!xp.OraQryIsGood) r1.qryInvalid(i,xp.file_tbl,"see report file below");
System.out.println("Submitting the "+xp.val_type+" validation process-> report file:

HlpValOraExtXml1 hvoex = new HlpValOraExtXml1(xp); <<<<<<<<<<<<<<<<<<<<<<<<<<<

int l = r1.getLineCnt(xp.rep_file);
if (l>8) {
String e=new String("Error -> See report in file:

System.out.println(e);
String sio=r1.io[i][3].toString();sio=sio.concat(e);r1.io[i][3]=new String(sio).trim();
}
else { r1.io[i][3]=new String(" ");}
} catch (FileNotFoundException fnfe){System.out.println("rep_file-FileNotFound:"+fnfe);}
} catch (Exception e) {System.out.println(e);}
}

if (r1.SPECD) System.out.println("HlpValXls1: here4");

if (xp.val_type.equals("HVPBX"))
{
if (r1.DEBUG) System.out.println("in HVPBX in HlpvalXls1");
try
{
try
{
FileOutputStream fos = new FileOutputStream(xp.rep_file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
if (r1.SPECD) System.out.println("HlpValXls1: here5");
if (!xp.XsdFilIsGood) {r1.xsdInvalid(i,xp.xsd_file,""); err1=true;}
if (!xp.DatFilIsGood) {r1.filInvalid(i,xp.dat_file," ->not Submitted"); err2=true;}
if (err1||err2) {}
else
{
System.out.println("Submitting the "+xp.val_type+" validation process-> report file:


HlpValPipBufXml1 hvpbx = new HlpValPipBufXml1(xp); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

int l = r1.getLineCnt(xp.rep_file);
if (l>8) {
String e=new String("Error -> See report in file:

System.out.println(e);
String sio=r1.io[i][3].toString();sio=sio.concat(e);r1.io[i][3]=new String(sio).trim();
}
else {r1.io[i][3]=new String(" ");}
}
} catch (FileNotFoundException fnfe){System.out.println("rep_file-FileNotFound:"+fnfe);}
} catch (Exception e) {System.out.println(e);}
}

if (xp.val_type.equals("HVPEX"))
{
if (r1.DEBUG) System.out.println("in HVPEX in HlpvalXls1");
try
{
try
{
FileOutputStream fos = new FileOutputStream(xp.rep_file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
if (!xp.XsdFilIsGood) {r1.xsdInvalid(i,xp.xsd_file,""); err1=true;}
if (!xp.DatFilIsGood) {r1.filInvalid(i,xp.dat_file," ->not Submitted"); err2=true;}
if (err1||err2) {}
else
{
System.out.println("Submitting the "+xp.val_type+" validation process-> report file:

HlpValPipExtXml1 hvpex = new HlpValPipExtXml1(xp); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

int l = r1.getLineCnt(xp.rep_file);
if (l>8) {
String e=new String("Error -> See report in file: p_file);
System.out.println(e);
String sio=r1.io[i][3].toString();sio=sio.concat(e);r1.io[i][3]=new String(sio).trim();
}
else {r1.io[i][3]=new String(" ");}
}
} catch (FileNotFoundException fnfe){System.out.println("rep_file-FileNotFound:"+fnfe);}
} catch (Exception e) {System.out.println(e);}

}
if (xp.val_type.equals("HVPJV")) { if (r1.DEBUG) System.out.println("in HVPJV in HlpvalXls1");
try
{
try
{
FileOutputStream fos = new FileOutputStream(xp.rep_file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
if (!xp.XsdFilIsGood) {r1.xsdInvalid(i,xp.xsd_file,""); err1=true;}
if (!xp.DatFilIsGood) {r1.filInvalid(i,xp.dat_file," ->not Submitted"); err2=true;}
if (err1||err2) {}
else
{
System.out.println("Submitting the "+xp.val_type+" validation process-> report file:

HlpValPipJavVal1 hvpjv = new HlpValPipJavVal1(xp); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

int l = r1.getLineCnt(xp.rep_file);
if (l>8) {
String e=new String("Error -> See report in file:
System.out.println(e);
String sio=r1.io[i][3].toString();sio=sio.concat(e);r1.io[i][3]=new String(sio).trim();
}
else {r1.io[i][3]=new String(" ");}
}
} catch (FileNotFoundException fnfe){System.out.println("rep_file-FileNotFound:"+fnfe);}

} catch (Exception e) {System.out.println(e);}
}

} // if "true"
} // for loop

[/code]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, change HlpValOraBufXml1 and the others to implement Runnable, change the constructor to store the "xp" in a member variable, add a run() method that does all the stuff the constructor used to do.

And start er up with

If these things run for days, you may run into trouble starting too many of them at once. If that sounds likely, ask about thread pools.

More general comments. This method is waaaay too long for me. See if you can write a method that describes the whole thing in a few lines, literally no longer than:

And second, variable names are waaaay to short unless you are deliberately obfuscating your company's business. I'd go nuts with r1, xp, sio, ps. The bits to spell out longer names are free.

Both of these tips follow from the rule: Write for human beings first, the compiler second.

It's sometimes hard to tell in written notes ... I'm not trying to be a hard case. Keep having fun, post what you come up with next!
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would echo Stan's comments and stay far away from people who wrote that code. Here is some framework around your task scheduler:

 
bob connolly
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, sounds very good! appreciate the help on this!

Have a nice weekend!

bc
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic