public class ThreadSafeProgressMonitor extends java.lang.Object implements ProgressMonitor
ProgressMonitor
to
make it thread safe.
Updates to the underlying ProgressMonitor are made only from the thread that
allocated this wrapper. Callers are responsible for ensuring the allocating
thread uses pollForUpdates()
or waitForCompletion()
to
update the underlying ProgressMonitor.
Only update(int)
, isCancelled()
, and endWorker()
may be invoked from a worker thread. All other methods of the ProgressMonitor
interface can only be called from the thread that allocates this wrapper.Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.locks.ReentrantLock |
lock |
private java.lang.Thread |
mainThread |
private java.util.concurrent.atomic.AtomicInteger |
pendingUpdates |
private ProgressMonitor |
pm |
private java.util.concurrent.Semaphore |
process |
private java.util.concurrent.atomic.AtomicInteger |
workers |
UNKNOWN
Constructor and Description |
---|
ThreadSafeProgressMonitor(ProgressMonitor pm)
Wrap a ProgressMonitor to be thread safe.
|
Modifier and Type | Method and Description |
---|---|
void |
beginTask(java.lang.String title,
int totalWork)
Begin processing a single task.
|
private void |
doUpdates() |
void |
endTask()
Finish the current task, so the next can begin.
|
void |
endWorker()
Notify the monitor a worker is finished.
|
boolean |
isCancelled()
Check for user task cancellation.
|
private boolean |
isMainThread() |
void |
pollForUpdates()
Non-blocking poll for pending updates.
|
void |
start(int totalTasks)
Advise the monitor of the total number of subtasks.
|
void |
startWorker()
Notify the monitor a worker is starting.
|
void |
startWorkers(int count)
Notify the monitor of workers starting.
|
void |
update(int completed)
Denote that some work units have been completed.
|
void |
waitForCompletion()
Process pending updates and wait for workers to finish.
|
private final ProgressMonitor pm
private final java.util.concurrent.locks.ReentrantLock lock
private final java.lang.Thread mainThread
private final java.util.concurrent.atomic.AtomicInteger workers
private final java.util.concurrent.atomic.AtomicInteger pendingUpdates
private final java.util.concurrent.Semaphore process
public ThreadSafeProgressMonitor(ProgressMonitor pm)
pm
- the underlying monitor to receive events.public void start(int totalTasks)
This should be invoked at most once per progress monitor interface.
start
in interface ProgressMonitor
totalTasks
- the total number of tasks the caller will need to complete
their processing.public void beginTask(java.lang.String title, int totalWork)
beginTask
in interface ProgressMonitor
title
- title to describe the task. Callers should publish these as
stable string constants that implementations could match
against for translation support.totalWork
- total number of work units the application will perform;
ProgressMonitor.UNKNOWN
if it cannot be predicted in advance.public void startWorker()
public void startWorkers(int count)
count
- the number of worker threads that are starting.public void endWorker()
public void pollForUpdates()
public void waitForCompletion() throws java.lang.InterruptedException
java.lang.InterruptedException
- if the main thread is interrupted while waiting for
completion of workers.private void doUpdates()
public void update(int completed)
This is an incremental update; if invoked once per work unit the correct
value for our argument is 1
, to indicate a single unit of
work has been finished by the caller.
update
in interface ProgressMonitor
completed
- the number of work units completed since the last call.public boolean isCancelled()
isCancelled
in interface ProgressMonitor
public void endTask()
endTask
in interface ProgressMonitor
private boolean isMainThread()