Java 8u161/8u162 makes Swing app use CPU

Brian S. :

When running a Swing application on 8u161 or 8u162 and focus is in a JTextField and you switch to another application (like Chrome) and back to the application CPU usage grows to 15% on my 8 core Windows 10 PC (as if an entire core is busy processing events).

Just run the application and switch a couple of times. If I click on the tab in the tabbed pane CPU usage drops to 0 as expected.

public class Test {
  public static void main(String... args) {
    SwingUtilities.invokeLater(() -> {
      JFrame f = new JFrame("Test");
      JTabbedPane tp = new JTabbedPane();
      tp.addTab("tab 1", new JTextField(20));
      f.add(tp);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

I have tried to look at the event queue to see what happens and it looks a lot like the last event gets processed again and again

If I add this to the above program I get a lot of java.awt.event.InvocationEvent[INVOCATION_DEFAULT,runnable=sun.awt.windows.WInputMethod ...

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueue(){
  @Override protected void dispatchEvent(AWTEvent event) {
    System.out.println(event);
    super.dispatchEvent(event);
  }
});

Works OK on 8u151, 8u152 and 9.0.4

I have a lot of customers that are upgrading to 161 and get this problem so any suggestions for a workaround is much appreciated. I have filed a bug with Oracle

JProfiler shows this: enter image description here

Seems to work OK on 8u172 b02

According to openjdk this was introduced by 8184016 and fixed by 8183504

ino2yoshi :

Are your customers using input methods? If you don't need input methods enabled, I suggest that you could disable it.

public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              JFrame f = new JFrame("Test");
              JTabbedPane tp = new JTabbedPane();
              JTextField tf = new JTextField();
              tf.enableInputMethods(false); // disable IM
              tp.addTab("tab 1", tf);
              f.add(tp);
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.pack();
              f.setLocationRelativeTo(null);
              f.setVisible(true);
            }
        });
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=429945&siteId=1