Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

composition with a run() method; new to threads

Thread view: 
hawat.thufir@gmail.com - 10 Jun 2005 14:28 GMT
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 - 11 Jun 2005 18:25 GMT
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?
hawat.thufir@gmail.com - 12 Jun 2005 04:01 GMT
> All you have to do to make that work is to have interface BasicTidy
> extend Runnable.
[quoted text clipped - 20 lines]
>
> //Hope that helps?

aha, yes, thank you.  very simple and I get to "avoid" dealing with
learning about threads.

-Thufir
Thomas G. Marshall - 21 Jun 2005 00:51 GMT
hawat.thufir@gmail.com coughed up:
>> All you have to do to make that work is to have interface BasicTidy
>> extend Runnable.
[quoted text clipped - 23 lines]
> aha, yes, thank you.  very simple and I get to "avoid" dealing with
> learning about threads.

You /never/ quite get away from that in java.  Nor should you.

Signature

"Well, ain't this place a geographical oddity!
Two weeks from everywhere!"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.