2011-01-08 120 views
0

我正在使用java。我試圖執行一個線程,但是我得到的問題是 thread.start()方法正在執行,但正如我們知道當我們調用線程的start方法時,將在內部調用run()方法。但在我的情況下,run()方法沒有得到執行。執行run()方法時出現問題

我正在使用sellinium。有沒有機會因硒而得到這個問題?

// in some another class 

公共靜態無效的主要(字串[] args)拋出異常{

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(); 
     } 

//線程類

類ScrapeThread {

公共ScrapeThread(字符串threadName){

thread = new Thread(this,threadName); 
    System.out.println(thread.getName()); 

} 

// run()的 公共無效的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); 
       } 

      } 
+0

不認爲硒會影響這一點。顯示一些代碼? – 2011-01-08 06:50:55

+0

其實我的線程的run()方法有一些selenium代碼作爲selenium.start()所以最新情況是當我直接調用run()方法時,硒工作正常,但當我做正常的線程執行,即時通訊調用thread.start ()方法,然後硒是給問題。什麼可能是問題先生..? – saggy 2011-01-08 12:30:48

+0

是ScrapeThread實現Runnable的嗎? – 2011-01-08 14:22:45

回答

0

你是怎麼知道的,不執行run方法。你是否在跑步方法上留下了痕跡?

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

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

試試我上面給出的新的;

1

查看代碼並將其與您的代碼進行比較。

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(); 
} 
0

只是粗略地回顧一下你的代碼......我看到你可能已經有點快樂了。考慮:

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可能會幫助你一點。