Threading / Concurrency

Discussion in 'Programming General' started by basterache3, Jun 14, 2011.

Threading / Concurrency
  1. Unread #1 - Jun 14, 2011 at 7:56 PM
  2. basterache3
    Joined:
    Jul 11, 2007
    Posts:
    86
    Referrals:
    0
    Sythe Gold:
    0

    basterache3 Member
    Banned

    Threading / Concurrency

    I have a java program I wrote to scrape a certain set of webpages.

    For example, I have a list of numerical IDs that need to be scraped. The base url is all the same.

    for example here are very simplified methods

    Code:
    public void prgm(){
         ArrayList<String> scrapedData = new ArrayList<String>();
         for(int i=0; i=100; i++){
              scrapedData.add(scrapePage(i));
         }
    }
    public String scrapePage(int id){
         //// do work here
         ///return a string
         return "abc";
    }
    
    Right now My program will scrape one page, finish, then scrape the next page, finish, and on and on

    It only will scrape one page at once.

    I want to change it so I can scrape 5 or 10 pages at once

    any suggestions?
     
  3. Unread #2 - Jun 15, 2011 at 4:34 AM
  4. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Threading / Concurrency

    Are you asking how to do it or what the best way of going about it would be?
     
  5. Unread #3 - Jun 15, 2011 at 11:54 AM
  6. basterache3
    Joined:
    Jul 11, 2007
    Posts:
    86
    Referrals:
    0
    Sythe Gold:
    0

    basterache3 Member
    Banned

    Threading / Concurrency

    I want to make 10 concurrent calls to the method scrapePage and need to know how to do it
     
  7. Unread #4 - Jun 15, 2011 at 12:46 PM
  8. wackywamba
    Joined:
    Jul 14, 2005
    Posts:
    1,358
    Referrals:
    0
    Sythe Gold:
    1

    wackywamba Guru

    Threading / Concurrency

    You'd want to use a thread.

    Basically, this is how you would go about it:

    Start by creating a class that extends thread
    Code:
    public class Scraper extends Thread{
    }
    
    If you're working in an IDE it will complain about invoking methods, this is because a thread needs a run method (the thing that you're actually threading) inside of your class that extends thread.
    Code:
    public void run(){
    }
    
    So basically put the code for scrapping inside of the run, then as you pop the "Scapers" off of the array list you just say scraper.start();

    Once you start them the run method will execute, hope this helps.
     
< Need help with java on computer | [HELP] Parseing in vb6 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site