RFR: 8286663: Resolve IDE warnings in WTrayIconPeer and SystemTray [v3]

Sergey Bylokhov serb at openjdk.java.net
Thu Jun 2 00:39:27 UTC 2022


On Thu, 2 Jun 2022 00:32:29 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> The IDE warning was changed based on the article above. Did you have a chance to reproduce a possible race caused by data change after the size() was called?
>
> I can reproduce it by this code example:
> 
> class Scratch {
> 
>     static volatile Throwable failed;
>     static volatile long endtime;
> 
>     public static void main(String[] args) throws Exception {
>         if (!SystemTray.isSupported()) {
>             return; // passed if SystemTray is not supported
>         }
>         SystemTray st = SystemTray.getSystemTray();
> 
>         endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
> 
>         Thread thread1 = new Thread(() -> {
>             while (!isComplete()) {
>                 try {
>                     st.add(new TrayIcon(new BufferedImage(10, 10,
>                                                           BufferedImage.TYPE_INT_ARGB)));
>                 } catch (AWTException e) {
>                     failed = e;
>                 }
>             }
>         });
>         Thread thread2 = new Thread(() -> {
>             while (!isComplete()) {
>                 try {
>                     st.add(new TrayIcon(new BufferedImage(10, 10,
>                                                           BufferedImage.TYPE_INT_ARGB)));
>                 } catch (AWTException e) {
>                     failed = e;
>                 }
>             }
>         });
>         Thread thread3 = new Thread(() -> {
>             while (!isComplete()) {
>                 TrayIcon[] icons = st.getTrayIcons();
>                 if (icons.length > 0) {
>                     st.remove(icons[0]);
>                 }
>             }
>         });
>         Thread thread4 = new Thread(() -> {
>             while (!isComplete()) {
>                 TrayIcon[] icons = st.getTrayIcons();
>                 if (icons.length > 0 && icons[icons.length - 1] == null) {
>                     failed = new RuntimeException(
>                             "icon is null:" + Arrays.toString(icons));
>                 }
>             }
>         });
>         thread1.start();
>         thread2.start();
>         thread3.start();
>         thread4.start();
>         thread1.join();
>         thread2.join();
>         thread3.join();
>         thread4.join();
>         for (TrayIcon icon : st.getTrayIcons()) {
>             st.remove(icon); // cleanup
>         }
>         if (failed != null) {
>             System.err.println("Test failed");
>             throw new RuntimeException(failed);
>         }
>     }
> 
>     private static boolean isComplete() {
>         return endtime - System.nanoTime() < 0 || failed != null;
>     }
> }

BTW I think that the usage of 
`return icons.toArray(EMPTY_TRAY_ARRAY);`
is slower than any of other cases: presized or zero. So if we would like to use a similar pattern the zero version is better.

-------------

PR: https://git.openjdk.java.net/jdk/pull/8850



More information about the client-libs-dev mailing list