Skip to content

Background jobs

Arik Hadas edited this page Jan 22, 2017 · 2 revisions

FolderChangeMonitor

The FolderChangeMonitor class is used to monitor changes on the current folder and to refresh that folder pane when a change occurs. Each folder pane has its own monitor. Each monitor has a pause flag. When the monitor is paused the folder pane is not refreshed.
A monitor is paused for a pane when a job is executed, so the folder is not refreshed during the job execution. When a job is being executed, the ProgressDialog is modal so you can't switch to another window to perform any other operations.
When we add an option to execute jobs in the background all monitors should be paused so that folders in different windows are not refreshed too frequently.
The pause flag is local to a folder pane, but we can make it global (static) to all panes. When the first job starts all pane updates will be paused, when the last job from a queue stops, the pause flag will be cleared, and an active pane will be refreshed.

FileJob

The FileJob class is a base class for all jobs.
This class is not thread safe now. Each job is executed in its own thread. There are several fields that are read and written by different threads. An example is getState/setState - a state of the job is updated in a job thread, but read in another thread (the thread that updates the UI). This method should be synchronized.
The setState method notifies all listeners about state change. However, there are two problems:

  • the iterator is not thread safe - when a listener removes itself the state of iterator is undefined
  • listeners perform their actions in the job thread. Listeners can block the job thread. The action executed by the listener will probably update some UI information (like ProgressDialog now) so I think they should be executed in the Swing thread.

I think some methods from FileJob could be marked as final so that descendants of this class cannot override them as not synchronized.
The methods that should be marked as synchronized are:

  • addFileJobListener, removeFileJobListener
  • getStartDate, getEndDate, getPauseStartDate, setPauseStartDate, getPausedTime, calcPausedTime, setPaused, getEffectiveJobTime, getCurrentFilename
  • getTotalPercentDone, getCurrentFileIndex, getCurrentFile, setCurrentFile, getNbFiles, setNbFiles, getStatusString interrupt, stop

The refreshTables method enables the folder change monitor. It should be enabled only when there are no active background jobs.

TransferFileJob

The TransferFileJob class is a base class for all copy/move jobs.
Methods getCurrentFileByteCounter, getTotalByteCounter, getTotalSkippedByteCounter return a ByteCounter class. It could be changed to return byte count (as long). The method resetCurrentFileByteCounter should be added to reset the byte counter.
Then we can synchronize these methods.
Other methods that should be marked as synchronized are:

  • getFilePercentDone,getCurrentFileSize,
  • setThroughputLimit/getThroughputLimit

UI changes

On the job progress dialog add "in background" button.
When a background job is active show a spinning icon to the left of the trash button.
Clicking on it shows a table with all background jobs. Clicking on a table row opens a progress dialog for a selected job.