mbien commented on code in PR #6514:
URL: https://github.com/apache/netbeans/pull/6514#discussion_r1348317921
##########
java/maven/src/org/netbeans/modules/maven/NbMavenProjectImpl.java:
##########
@@ -559,29 +588,131 @@ void stopHardReferencingMavenPoject() {
return newproject;
}
+ /**
+ * Task that potential project reloads should wait on. If set, a {@link
fireProjectReload}(true) will be scheduled only after this blocker finishes.
+ */
+ private RequestProcessor.Task reloadBlocker;
-
+ /**
+ * Schedules project operation that delays potential reloads. If a reload
is posted, it will be performed only after
+ * this operation compeltes (successfully, or erroneously). Multiple
project operations can be scheduled, an eventual project reload
+ * should happen after all those operations complete. It is possible to
postpone project reload indefinitely, avoid unnecessary
+ * operation schedules.
+ * <p>
+ * To avoid race condition on task startup, this method actually creates
and schedules the task so it blocks reloads from its inception.
+ * It returns the value of the worker task as the result value.
+ * wrapper.
+ * @param rp request processor that should schedule the task
+ * @param delay optional delay, use 0 for immediate run
+ * @param r operation to run
+ * @return the scheduled task
+ */
+ public RequestProcessor.Task scheduleProjectOperation(RequestProcessor rp,
Runnable r, int delay) {
+ RequestProcessor.Task t = rp.create(r);
+ if (Boolean.getBoolean("test.reload.sync")) {
+ LOG.log(Level.FINE, "Running the blocking task synchronously
(test.reload.sync set)");
+ t.run();
+ return t;
+ } else {
+ synchronized (this) {
+ if (reloadBlocker == null) {
+ LOG.log(Level.FINER, "Blocking project reload on task
{0}", t);
+ reloadBlocker = t;
+ } else {
+ // will chain after existing reload blocker AND the new
task.
+ final RequestProcessor.Task t2 = RELOAD_RP.create(() ->
{});
+ LOG.log(Level.FINER, "Creating project blocker {0}, chain
after existing blocker {1}", new Object[] { t2, t });
+ reloadBlocker.addTaskListener((e) -> {
+ t.addTaskListener((e2) -> {
+ synchronized (NbMavenProjectImpl.this) {
+ if (t2 == reloadBlocker) {
+ reloadBlocker = null;
+ }
+ }
+ t2.run();
+ });
+ });
+ reloadBlocker = t2;
+ }
+ }
+ t.schedule(delay);
+ return t;
Review Comment:
Lets say one thread runs the ops sequentially, then tasks could be simply
put into a single queue: `op0, op1, reload, op2`
This could even optimize the queue by only allowing one reload. If a second
one is posted the other reload op is removed, so that it is always at the end
of the queue.
If ops run in parallel however:
```
|--op0------------|
|--op1----|
|--op2-------|
```
and someone schedules a reload, which task is the blocker? This is difficult
to track with an event system and one single variable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists