mbien commented on code in PR #6514:
URL: https://github.com/apache/netbeans/pull/6514#discussion_r1348618838


##########
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:
   i can take a closer look at it this weekend. but you could also queue all 
tasks and also the reload task on one sequential RP with 1 thread. It is 
probably the safest and simplest approach. If this turns out to be a 
performance bottleneck we can think about parallelizing this later.



-- 
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

Reply via email to