title: Java Async Data Load with progress and Threadding nightmares
body: <p>All,</p>
  
  <p>We are having problems with data loading in our application.  The purpose of the current implementation was to have an asynchronous mechanism for data loading.  </p>
  
  <p><strong>I basically want to know if there are any good patterns or practices for this.</strong></p>
  
  <p>The application has many different UI components that is not coded in a standard way.  It is all generated from configuration (i.e. xml).  The data load is triggered by clicking on a calendar, which would send an asynchronous message to the data loaders, of which there are around 6 instances (of the same class) which would load the data from the database.  Once the data has been loaded and processed, the data loader class would then send the table data asynchronously to the JTable it is to be displayed in.</p>
  
  <p>As the data load can take anywhere from a second or so to 1 or 2 minutes, a progress dialog was required.  An additional class was written to listen for events from the data loaders when the start/complete the data load.  When any data loader starts loading, a dialog is displayed indicating that the data was loading.  When the last data loader had completed, the dialog should be hidden.  </p>
  
  <p><strong>Current Problems/Issues</strong></p>
  
  <p>There are 2 problems that occur:</p>
  
  <ul>
  <li>Sometimes the dialog will remain visible, even though all of the data loaders have completed.   </li>
  <li>Othertimes, the data may not be displayed in the table.</li>
  </ul>
  
  <p>We have tried in vain to add the synchronized keyword around the showing and hiding methods of the progress, but still get similar issued.  I think some kind of rewrite may be in order, but I want to understand a little more on async java patterns.  The mechanism needs to be more robust and reliable.</p>
  
  <p><strong>Current Process</strong></p>
  
  <p>If your interested in our current process, I have tried to summarise the events below.  This might shed some light on problems in design/implementation.</p>
  
  <ol>
  <li><p>User clicks on day in calendar.</p>
  
  <p>The calendar component sends a message to each data loader instance telling them to start loading.  This message is sent from the calendar asynchronously on a separate thread (2).</p></li>
  <li><p>The Data Loader receives the load message</p>
  
  <p>Each DataLoader is responsible for loading relevant data and passing it to a table to be displayed.</p>
  
  <p>Each data loader has a single thread instance on which the data is loaded.  If the current thread load instance is set and is loading data, then the loading is aborted - which might occur from a quick click of days in the calendar.  A new thread instance is created and started once the data loading thread is idle or null.</p>
  
  <p>The data load thread then performs the data access and processing.  When it is started, the DataLoadDialog class is notified (3).  This DataLoadDialog basically adds a listener (DataLoaderListener extends EventListener) to a EventListenerList.  When the data thread loads, the DataLoaderListener.loadStart is invoked, which is picked up by the DataLoadDialog.  When the thread has loaded and processed the data, the DataLoaderListener.loadComplete is called.  This is again picked up in the DataLoadDialog.</p>
  
  <p>Now that the data is loaded, the DataLoader sends the data to the table to be displayed.</p></li>
  <li><p>Inform DataLoadDialog of load status</p>
  
  <p>The DataLoadDialog has a reference to each data loader.  Each data loader has a status, which is accessed via a getter, which returns an enum of Idle or Loading.  Whenever the DataLoadDialog receives an invokation of the loadStart or loadComplete, it determines whether or not to show/hide the progress dialog, depending upon the state of the data load threads.</p>
  
  <p>The process dialog is a singleton static instance which is created when the class in created (from the configuration xml).</p></li>
  </ol>
  
  <p>Any help would be appreciated.  If I have missed anything then please add comment and I will update the question.</p>
  
  <p>Thanks,</p>
  
  <p>Andez</p>