Changeset 17526

Show
Ignore:
Timestamp:
13.10.2008 21:44:43 (11 years ago)
Author:
ak19
Message:

Dr Bainbridge adjusted code to replace the variable called busy which was used in a processor-intensive loop and causing problems when terminating wget from the Download panel when more than once download instance was running. The solution was to use Thread's wait and notify (see also DownloadJob?.java).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/download/DownloadScrollPane.java

    r12529 r17526  
    5252    extends Thread { 
    5353     
    54     /** <i>true</i> if there is a task currently being carried out, <i>false</i> otherwise. */ 
    55     private boolean busy = false; 
    5654    /** <i>true</i> if verbose debug messages should be displayed, <i>false</i> otherwise. */ 
    5755    private boolean debug = false; 
     
    6967    private Vector job_queue; 
    7068    static final private boolean simple = true; 
    71      
     69 
     70         
    7271    public DownloadScrollPane() { 
    7372    job = null; 
     
    8180    public void deleteDownloadJob(DownloadJob delete_me) { 
    8281    if (delete_me == job) { 
    83         while(busy) { 
    84         } 
     82        try { 
     83        // Wait for the job to finish cleaning up, before we can continue cleaning up here. 
     84        // But sometimes the job has completed (terminated naturally) and in that case there's 
     85        // nothing to wait for. 
     86        synchronized(delete_me) { 
     87            if (!delete_me.hasSignalledStop()) { // don't wait if DownloadJob.COMPLETED  
     88            delete_me.wait(); 
     89            } 
     90        } 
     91        } catch (Exception e) { 
     92        e.printStackTrace(); 
     93        } 
     94         
    8595        job = null; 
    86     } 
     96         
     97    } 
     98    // Get rid of this download's dedicated pane 
     99    finishedDownloadJob(delete_me); 
     100    } 
     101 
     102    /** To be called when a download job has terminated naturally or was prematurely stopped 
     103     *  via the close button. Gets rid of this download's pane with buttons and progress bar. */ 
     104    protected void finishedDownloadJob(DownloadJob delete_me) { 
    87105    if (delete_me.hasSignalledStop()) { 
    88106        list_pane.remove(delete_me.getProgressBar()); 
     
    131149 
    132150    DebugStream.println("About to create a new job"); 
    133  
    134  
     151     
    135152        DownloadJob new_job = new DownloadJob(download, Configuration.proxy_pass, Configuration.proxy_user, this, mode, proxy_url); 
    136153    // Tell it to run as soon as possible 
    137  
    138        
    139154 
    140155    new_job.setState(DownloadJob.RUNNING); 
     
    165180    //list_pane.setAlignmentX(Component.LEFT_ALIGNMENT); 
    166181    list_pane.updateUI(); 
    167     new_job = null; 
     182    new_job = null;         //job = (DownloadJob) job_queue.get(index); 
     183 
    168184    synchronized(this) { 
    169185        notify(); // Just incase its sleeping. 
     
    189205    while(true) { 
    190206        if(job_queue.size() > 0) { 
    191         int index = 0; 
    192         while(job_queue.size() > 0 && index < job_queue.size()) { 
    193             job = (DownloadJob) job_queue.get(index); 
     207        while(!job_queue.isEmpty()) {  
     208            job = (DownloadJob) job_queue.firstElement();  
     209             
    194210            if(job.getState() == DownloadJob.RUNNING) { 
    195             DebugStream.println("DownloadJob " + job.toString() + " Begun."); 
    196             busy = true; 
     211            DownloadJob delete_me = job; 
     212            String jobDisplayString = job.toString(); 
     213            DebugStream.println("DownloadJob " + jobDisplayString + " Begun."); 
     214            System.err.println("DownloadJob " + job.port + " " + job.toString() + " Begun."); 
    197215            job.callDownload(); 
    198             busy = false; 
    199             DebugStream.println("DownloadJob " + job.toString() + " complete."); 
     216            finishedDownloadJob(delete_me); // Job is done, get rid of the separate display panel for this download 
     217            System.err.println("DownloadJob " + jobDisplayString + " complete."); 
     218            DebugStream.println("DownloadJob " + jobDisplayString + " complete."); // by this point job is null! 
    200219            job = null; 
    201             } 
    202             index++; 
     220            delete_me = null; 
     221            } 
    203222        } 
    204223        try {