Add documentation
This commit is contained in:
parent
2e2ec6ff68
commit
52b09474ba
@ -15,23 +15,37 @@ public class ScheduledTaskLifecycle {
|
|||||||
this.lock = new StampedLock();
|
this.lock = new StampedLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a scheduled task
|
||||||
|
*/
|
||||||
public void registerScheduledTask(ScheduledFuture<?> task) {
|
public void registerScheduledTask(ScheduledFuture<?> task) {
|
||||||
this.tasks.put(task, new Object());
|
this.tasks.put(task, new Object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark this task as running.
|
||||||
|
* After calling this method, please call {@method endScheduledTask} inside a finally block!
|
||||||
|
*/
|
||||||
public void startScheduledTask() {
|
public void startScheduledTask() {
|
||||||
this.lock.readLock();
|
this.lock.readLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mark this task as ended. Must be called after {@method startScheduledTask}
|
||||||
|
*/
|
||||||
public void endScheduledTask() {
|
public void endScheduledTask() {
|
||||||
this.lock.tryUnlockRead();
|
this.lock.tryUnlockRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancel all scheduled tasks and wait all running methods to finish
|
||||||
|
*/
|
||||||
public void cancelAndWait() {
|
public void cancelAndWait() {
|
||||||
tasks.forEach((task, obj) -> {
|
tasks.forEach((task, obj) -> {
|
||||||
task.cancel(false);
|
task.cancel(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Acquire a write lock to wait all tasks to end
|
||||||
lock.unlockWrite(lock.writeLock());
|
lock.unlockWrite(lock.writeLock());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user