Changeset 11563


Ignore:
Timestamp:
2006-04-03T09:58:49+12:00 (18 years ago)
Author:
shaoqun
Message:

changed the code handling threads to make it threads safe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/DownloadUrls.java

    r11472 r11563  
    66import java.util.*;
    77
    8 // import javax.swing.ImageIcon; //****
     8import javax.swing.ImageIcon; //****
    99
    1010
     
    4848
    4949    /** A static delay used when attempting to download more images into a full downloading buffer */
    50     final int delay_       = 1000;
     50    final int delay_       = 3000;
    5151    /** The maximum number of images to have downloaded and not yet displayed */
    52     final int buffer_size_ = 10;
     52    final int buffer_size_ = 1;
    5353
    5454    /** Used in cases where the image maps to a url outside of it's original location.
     
    6464
    6565    int verbosity_ = 0;
     66
     67    protected boolean busy_ = false;
     68   
     69    MediaTracker tracker;
    6670
    6771    /** Constructor to initialise a download thread from which images are found,
     
    7882            DownloadImages download_images, String starting_url,
    7983            String href_musthave, String image_mustnothave,
    80             String image_ignore, String image_type, String document_root,int verbosity)
     84            String image_ignore, String image_type, String document_root,int verbosity, MediaTracker trk)
    8185    {
    8286    super("DownloadUrls");
     
    9195    document_root_ = document_root;
    9296    verbosity_ = verbosity;
    93      
     97        tracker = trk;
    9498
    9599    System.err.println("starting_url_ "  + starting_url +"\n"+
     
    106110    }
    107111
    108    
     112      public boolean getStatus(){
     113    return busy_;
     114
     115    }
    109116
    110117    /** Determines whether or not a url has already been examined
     
    316323    public void add_image(URL url, String from_url, String img_name)
    317324    {
    318     try {
    319 
    320         boolean had_to_wait = false;
    321 
    322         // ensure that we don't download too many images
    323         while (download_images_.size() >= buffer_size_)
    324         {
    325         had_to_wait = true;
    326         Thread.sleep(delay_);
    327         }
    328        
    329325        // get the image from the url
    330326        if (verbosity_>=2) {
    331         System.err.println("  Downloading image URL: " + url.toString());
    332         }
    333 
    334         //ImageIcon image_icon = new ImageIcon(url);
    335         // Image image = image_icon.getImage();
    336 
    337         // Image image = Toolkit.getDefaultToolkit().createImage(url);
    338         // Image image = app_.getImage(url);
    339         Image image = Toolkit.getDefaultToolkit().getImage(url);
    340 
    341         //System.err.println("###DownloadingED image URL: " + url.toString());
    342 
    343         boolean status = app_.prepareImage(image,app_);
    344         //System.err.println(" Prepare Image status = " + status);
    345 
    346         Thread.sleep(100);
    347 
    348         // push image onto the downloaded images
    349         /* System.err.println("*** Pushing: name="+img_name
    350                    +" dimensions = "+image_icon.getIconWidth()+"x"+image_icon.getIconHeight());
    351         //                 +" dimensions = "+image.getWidth(app_)+"x"+image.getHeight(app_));
    352         */
    353 
    354         download_images_.push(image,from_url, img_name);
    355        
    356         // if have completed the maximum number of downloads for the
    357         // application then stop
    358         if (download_images_.size() == app_.maxDownloads()) {
    359         stop();
    360         }
    361        
    362     }
    363     catch (Exception e) {
    364         thread_running_ = false;
    365         stop();
    366         e.printStackTrace();
    367     }
     327        //System.err.println("  ****Downloading image URL: " + url.toString());
     328        }
     329
     330       int size = download_images_.downloadImage(tracker,url, from_url, img_name);
     331         
     332       try{
     333           // if have completed the maximum number of downloads for the
     334           // application then stop
     335           if (size == app_.maxDownloads()) {
     336           stop();
     337           }
     338           
     339       }
     340       catch (Exception e) {
     341           thread_running_ = false;
     342           stop();
     343           e.printStackTrace();
     344       }
    368345    }
    369346
     
    378355    {
    379356
    380        
    381     System.err.println("Parsing url =  " + new_url);
    382    
    383357    if (already_visited(new_url)) return;
    384358
     
    410384        for (int i = 0; i < src_links.size(); i++)
    411385        {
    412         URL url = (URL)src_links.elementAt(i);
     386        URL url = (URL)src_links.get(i);
    413387        String url_string  = url.toString();
    414388               
    415389        //System.err.println(" source links " + i + " ["  + url_string +"]");
    416390
    417         if (verbosity_ >= 3) {
     391        if (verbosity_ >= 4) {
    418392            System.err.println("    Unfiltered: src_link[" + i + "] = " + url_string);
    419393        }
     
    466440        for (int i = 0; i < href_links.size(); i++)
    467441        {
    468             URL url = (URL)href_links.elementAt(i);
     442            URL url = (URL)href_links.get(i);
    469443            String url_string  = url.toString();
    470444
     
    497471                if (filter_href(url_string,new_url,depth))
    498472                {
    499            
    500                                     System.out.println("*************************************");
    501473                    rec_add_images(url_string,depth+1);
    502474                   
     
    573545   
    574546        rec_add_images(starting_url_,1);
    575 
     547    download_images_.stopDownload();
    576548    System.err.println("Download thread finished.");
    577549    }
Note: See TracChangeset for help on using the changeset viewer.