aspose file tools*
The moose likes Threads and Synchronization and the fly likes Can i convert this code to use threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can i convert this code to use threads?" Watch "Can i convert this code to use threads?" New topic
Author

Can i convert this code to use threads?

bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
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

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Mar 10, 2004
Posts: 204
Thanks all, sounds very good! appreciate the help on this!

Have a nice weekend!

bc
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads and Synchronization...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can i convert this code to use threads?