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 Wed, 1 Jun 2022 23:13:57 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> I remember IDEs used to suggest using correctly sized arrays in the calls to `toArray`. Now they suggest using zero-sized array.
>> 
>> My idea was to resolve the warning raised by the IDE to reduce the number of yellow highlights on the file outline.
>> 
>> I agree the performance gain if any is negligible in this case: we don't expect many icons, probably no more than 3.
>
> 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;
    }
}

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

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



More information about the client-libs-dev mailing list