pepness commented on code in PR #6596:
URL: https://github.com/apache/netbeans/pull/6596#discussion_r1372028474


##########
enterprise/glassfish.common/src/org/netbeans/modules/glassfish/common/LogViewMgr.java:
##########
@@ -135,14 +147,11 @@ private LogViewMgr(final String uri) {
      */
     public static LogViewMgr getInstance(String uri) {
         LogViewMgr logViewMgr;
-        synchronized (instances) {
-            WeakReference<LogViewMgr> viewRef = instances.get(uri);
-            logViewMgr = viewRef != null ? viewRef.get() : null;
-            if(logViewMgr == null) {
-                logViewMgr = new LogViewMgr(uri);
-                instances.put(uri, new WeakReference<LogViewMgr>(logViewMgr));
-            }
-        }
+        
+        logViewMgr = instances.computeIfAbsent(uri, key -> 
+                        new WeakReference<LogViewMgr>(new LogViewMgr(uri))
+                    ).get();

Review Comment:
   Will this fix the hard reference issue?
   ```java
   public static LogViewMgr getInstance(String uri) {
       LogViewMgr logViewMgr;
       logViewMgr = instances.computeIfAbsent(uri, (String key) -> {
                   LogViewMgr _logViewMgr = new LogViewMgr(uri);
                   WeakReference<LogViewMgr> _viewRef = new 
WeakReference<>(_logViewMgr);
                   return _viewRef;
               }).get();
   return logViewMgr;
   ```
   Where is the race problem @matthiasblaesing? The `ConcurrentHashMap` 
implementation of `computeIfAbsent` is  atomic and concurrent.
   
[ConcurrentHashMap.computeIfAbsent](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent(K,java.util.function.Function))
   > The entire method invocation is performed atomically. The supplied 
function is invoked exactly once per invocation of this method if the key is 
absent, else not at all. Some attempted update operations on this map by other 
threads may be blocked while computation is in progress, so the computation 
should be short and simple.
   
   This will be executed in a `synchronized` block at a Node level instead of 
synchronizing the entire map.
   



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