saggy
saggy

Reputation: 13

Method run() is not executed

I'm using java. I'm trying to execute a thread, but the issue I'm getting is thread.start() method is getting executed, but as we know when we call the start method of thread, the run() method gets called internally. But in my case the run() method is not getting executed:

public static void main(String[] args) throws Exception {
    parseArguments(args);

    ScraperStore scraperStore = ScraperStore.getInstance();
    SocialSiteManager siteManager = new SocialSiteManager();
    sitesToScrape = siteManager.getSocialSitesToScrape();

    for (SocialSite site : sitesToScrape) {
        ScrapeThread srThread = new ScrapeThread("srThread");
        Thread scraper = new Thread(srThread);
        srThread.setSiteToScrape(site);
        srThread.setPageTypeToScrape(startPageToScrape);
        srThread.setTypeToScrape(typeToScrape);
        ArrayList<String> listOfValues = ScraperStore.getNextUrlToScrape(startPageToScrape, site);
        srThread.setTypeToScrape(typeToScrape);

        try {
            srThread.setUrlOwnedBy(listOfValues.get(0));
            srThread.setStartUrl(listOfValues.get(1));
            scraper.start();

            boolean state = scraper.isAlive();
            scrapeThreads.add(scraper);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}    

Thread class:

class ScrapeThread {

    public ScrapeThread(String threadName) {
        thread = new Thread(this,threadName);
        System.out.println(thread.getName());
    }

}

Run method:

public void run() {
    try {
        System.out.println("in the run method");
        selenium = new DefaultSelenium(config.getHost(), Integer.parseInt(config.getPort()), 
                config.getBrowser(), config.getUrl());
        selenium.start();
        Integer count = 0;

        while (startUrl != null) {
            HtmlPage homePage = new HtmlPage();
            homePage.setCreatedBy(new String());
            homePage.setCreatedon(new String());
            homePage.setModifiedBy(new String());
            homePage.setModifiedOn(new String());
            homePage.setNoOfItemsFound(new String());
            homePage.setOwnedBy(urlOwnedBy);
            homePage.setPageType(scraper.getPageTypeToScrape());
            homePage.setPageUrl(startUrl);

            proxy = getInitialisedProxy();
            scraper.setNavigator(proxy.getNavigator());
            scraper.setStartUrl(startUrl);

            try {
                scraper.initialize();
            } catch (MyException e) {
                if (status == false){
                    throw new Exception(MyException.NOTFOUND);
                }
            }
        }
    }
}

I'm using sellinium. Is there any chance that I'm getting the issue because of selenium?

Upvotes: 0

Views: 199

Answers (3)

seth
seth

Reputation: 83

Just from a cursory review of your code... I see that you might have gone a little thread-happy. Consider:

ScrapeThread srThread = new ScrapeThread("srThread"); // This is creating your ScrapeThread object (which should really implement the Runnable interface)
    Thread scraper = new Thread(srThread); // This is creating a thread which wraps another thread... take this out.
    srThread.setSiteToScrape(site);
    srThread.setPageTypeToScrape(startPageToScrape);
    srThread.setTypeToScrape(typeToScrape);
    ArrayList<String> listOfValues =              ScraperStore.getNextUrlToScrape(startPageToScrape, site);
        srThread.setTypeToScrape(typeToScrape);
        try {
            srThread.setUrlOwnedBy(listOfValues.get(0));
            srThread.setStartUrl(listOfValues.get(1));
            scraper.start(); // You would want to replace this with srThread.start(); once fixing the items I addressed above
            boolean state=scraper.isAlive();
            scrapeThreads.add(scraper);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

http://www.javabeginner.com/learn-java/java-threads-tutorial might help you out a bit.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

Look at code and compare it with your code.

public static void main(String []args)
{
   Runnable inst=new Runnable()
    {
      public void run()
        {
           System.out.println("Thread statement!");
         }
     };

   Thread thrd=new Thread(inst);
   thrd.start();
}

Upvotes: 1

Dead Programmer
Dead Programmer

Reputation: 12575

How did you come to know run method is not executed . didu u put a trace on the run method?

//Old
new Thread(niidleThread,"scraper"); scraper.start()

// new
new Thread(srThread);  or
new Thread(srThread,"scraper");  

Try the new one i have given above;

Upvotes: 0

Related Questions