Thread Sample V


package pagefinder;

import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nurhak
 */
public class Worker extends Thread {

    private String threadName;
    private int startNumber;
    private int finishNumber;

    public Worker(String threadName, int startNumber, int finishNumber) {
        System.out.println(threadName+ " has started!");
        this.threadName = threadName;
        this.startNumber = startNumber;
        this.finishNumber = finishNumber;
    }

    @Override
    public void run() {
        for (int i = startNumber; i <finishNumber+1; i++) {
            try {
                URL pageUrl = new URL("http://www.samplesite/" + i + ".html");
                URLConnection pageConn = pageUrl.openConnection();
                pageConn.connect();
                String pageHeader = pageConn.getHeaderField(0);

                if ((pageHeader.indexOf("404")) != -1) {
                    System.out.println("Page not found!");
                } else {
                    System.out.println(threadName + " -->found = " + i + " - ");
                }
            } catch (Exception ex) {
                Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


package pagefinder;

/**
 *
 * @author Nurhak
 */
public class PageFinder {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Worker("Thread1", 0, 100).start();
        new Worker("Thread2", 100, 200).start();
        new Worker("Thread3", 200, 300).start();
        new Worker("Thread4", 300, 400).start();
        new Worker("Thread4", 400, 500).start();

        System.out.println("All threads have been started!");
    }
}

Comments

Popular posts from this blog

Solution for "Can not obtain Oracle client information from registry"

Solution for the fault: "System.Diagnostics.Process.Start is not working on IIS, but working on ASP.NET Development Server"

Getting Umbraco Content - IPublishedContent vs IContent vs Node vs Document