1 /* 2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 /* 26 @test 27 @bug 8043610 28 @summary Tests that JComponent invalidate, revalidate and repaint methods could 29 be called from any thread 30 @author Petr Pchelko 31 @modules java.desktop/sun.awt 32 */ 33 34 import sun.awt.SunToolkit; 35 36 import javax.swing.*; 37 import java.awt.*; 38 import java.util.concurrent.CountDownLatch; 39 import java.util.concurrent.atomic.AtomicReference; 40 41 public class bug8043610 { 42 private static volatile JFrame frame; 43 private static volatile JComponent component; 44 45 public static void main(String[] args) throws Exception { 46 ThreadGroup stubTG = new ThreadGroup(getRootThreadGroup(), "Stub Thread Group"); 47 ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG"); 48 try { 49 Thread stubThread = new Thread(stubTG, SunToolkit::createNewAppContext); 50 stubThread.start(); 51 stubThread.join(); 52 53 CountDownLatch startSwingLatch = new CountDownLatch(1); 54 new Thread(swingTG, () -> { 55 SunToolkit.createNewAppContext(); 56 SwingUtilities.invokeLater(() -> { 57 frame = new JFrame(); 58 component = new JLabel("Test Text"); 59 frame.add(component); 60 frame.setBounds(100, 100, 100, 100); 61 frame.setVisible(true); 62 startSwingLatch.countDown(); 63 }); 64 }).start(); 65 startSwingLatch.await(); 66 67 AtomicReference<Exception> caughtException = new AtomicReference<>(); 68 Thread checkThread = new Thread(getRootThreadGroup(), () -> { 69 try { 70 component.invalidate(); 71 component.revalidate(); 72 component.repaint(new Rectangle(0, 0, 0, 0)); 73 } catch (Exception e) { 74 caughtException.set(e); 75 } 76 }); 77 checkThread.start(); 78 checkThread.join(); 79 80 if (caughtException.get() != null) { 81 throw new RuntimeException("Failed. Caught exception!", caughtException.get()); 82 } 83 } finally { 84 new Thread(swingTG, () -> SwingUtilities.invokeLater(() -> { 85 if (frame != null) { 86 frame.dispose(); 87 } 88 })).start(); 89 } 90 } 91 92 private static ThreadGroup getRootThreadGroup() { 93 ThreadGroup currentTG = Thread.currentThread().getThreadGroup(); 94 ThreadGroup parentTG = currentTG.getParent(); 95 while (parentTG != null) { 96 currentTG = parentTG; 97 parentTG = currentTG.getParent(); 98 } 99 return currentTG; 100 } 101 }