eirikbakke commented on PR #7668:
URL: https://github.com/apache/netbeans/pull/7668#issuecomment-2309134233

   @matthiasblaesing Yeah, it's not officially documented in the Javadoc for 
the java.awt.datatransfer.Clipboard class, but reading through its source code, 
it's clear that it is intended to be thread-safe, and NetBeans already assumes 
this to be the case (see the GetContents and SetContents tasks, which are run 
on a separate RequestProcessor thread). The class does I/O operations that may 
block for several seconds, so it can't be used purely purely on the event 
dispatch thread without regularly locking up the UI.
   
   In the Clipboard.addFlavorListener/removeFlavorListener case, you can call 
either method from any thread, but the actual events will be delivered on the 
Event Dispatch Thread. The fact that addFlavorListener/removeFlavorListener can 
block for several seconds (due to lock contention with the I/O-heavy getContent 
operation) was almost certainly unintentional, but calling them off-thread 
avoids the problem of locking up the UI.
   
   Reviewing the logic again, I can try to justify why the proposed code is 
safe...
   * A race condition bug around the systemClipboard.addFlavorListener call is 
avoided by ensuring that fireChange() is called only _after_ the 
systemClipboard.addFlavorListener has returned (to ensure that we don't "miss" 
a fireChange() call), and by confirming that fireChange() does no harm if 
called an extra time (it looks fine).
   * A race condition bug around the systemClipboard.removeFlavorListener call 
is avoided by the fact that the RequestProcessor RP has a throughput of one 
(thus adding/removing listeners will happen in order), and the fact that extra 
calls to flavorsChanged do no harm.
   * Looking at flavorsChanged, I see that it skips the call to fireChange() if 
anyWindowIsActivated == false. I confirmed that whenever anyWindowIsActivated 
is set to true (in the handler for WINDOW_ACTIVATED), this is followed by a new 
call to fireChange (via scheduleGetFromSystemClipboard(true) and 
GetContents.run).


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