For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2005 > composition with a run() method; new to threads









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author composition with a run() method; new to threads
hawat.thufir@gmail.com

2005-06-10, 4:02 pm

In ControlTidy I want to create an instance of Test16, but of BasicTidy
type, like:

BasicTidy aTidy = new Test16();

and then just work with the aTidy object.


someone's suggested to me that "It might be a better design if you
moved that run() function from Test16 to a Test16Runner that uses a
Test16," but I'm not sure what that means.


package atreides.tidyXhtml;

public interface BasicTidy{
public void setUrl(String url);
public void setOutFileName(String outFileName);
public void setErrOutFileName(String errOutFileName);
public void setXmlOut(boolean xmlOut);
}//BasicTidy



public class ControlTidy{


public static void main (String[] args) {
Test16 aTidy = new Test16();
String yahooUrl = "http://www.yahoo.com/";
String googleUrl = "http://www.google.com/";
String out = "out.txt";
String err = "err.txt";
boolean xml = true;

aTidy.setUrl (yahooUrl);
aTidy.setOutFileName (out);
aTidy.setErrOutFileName (err);
aTidy.setXmlOut(xml);

Thread aTidyThread = new Thread(aTidy);
aTidyThread.start();
}//main
}//ControlTidy



package atreides.tidyXhtml;
//"inspired" from source at jtidy on sourceforge.net
import java.io.IOException;
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.FileWriter;
import org.w3c.tidy.Tidy;


public class Test16 implements Runnable, BasicTidy{

private String url;
private String outFileName;
private String errOutFileName;
private boolean xmlOut;

public Test16(){
this.url = "http://www.google.com/";
this.outFileName = "out.txt";
this.errOutFileName = "err.txt";
this.xmlOut = true;
}//Test16

public void run() {
URL u;
BufferedInputStream in;
FileOutputStream out;
Tidy tidy = new Tidy();

tidy.setXmlOut(xmlOut);
try {
tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName),
true));
u = new URL(url);
in = new BufferedInputStream(u.openStream());
out = new FileOutputStream(outFileName);
tidy.parse(in, out);
}//try
catch ( IOException e ) {
System.out.println( this.toString() + e.toString() );
}//catch
}//run

public void setUrl (String url){
this.url = url;
}//setUrl

public void setOutFileName (String outFileName){
this.outFileName = outFileName;
}//setOutFileName

public void setErrOutFileName (String errOutFileName){
this.errOutFileName = errOutFileName;
}//setErrOutFileName

public void setXmlOut (boolean xmlOut) {
this.xmlOut = xmlOut;
}//setXmlOut

public static void main( String[] args ) {
}//main
}//Test16



thanks,

Thufir

ninja java

2005-06-11, 3:58 pm

All you have to do to make that work is to have interface BasicTidy
extend Runnable.

public interface BasicTidy {...}
//becomes
public interface BasicTidy extends Runnable {...}

that's it!!!


//If you want you can now change
Test16 aTidy = new Test16();
//into
BasicTidy aTidy = new Test16();
//in the main method; The "Thread aTidyThread = new Thread(aTidy); "
will still work because now BasicTidy extends Runnable

//You could also change
public class Test16 implements Runnable, BasicTidy {...}
//into
public class Test16 implements BasicTidy {...}
//because BasicTidy would have already implemented Runnable.
//That means you can use either class/interface for "aTidy".

//Hope that helps?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com