7122796: SunToolkit constructor should create the EventQueue for the Main AppContext
Summary: Always create an EQ for the main AppContext in SunToolkit constructor
Reviewed-by: art
--- a/jdk/src/share/classes/sun/awt/AppContext.java Mon Dec 19 16:44:36 2011 +0400
+++ b/jdk/src/share/classes/sun/awt/AppContext.java Tue Dec 20 12:48:01 2011 +0300
@@ -342,6 +342,16 @@
return appContext;
}
+ /**
+ * Returns the main ("system") AppContext.
+ *
+ * @return the main AppContext
+ * @since 1.8
+ */
+ final static AppContext getMainAppContext() {
+ return mainAppContext;
+ }
+
private long DISPOSAL_TIMEOUT = 5000; // Default to 5-second timeout
// for disposal of all Frames
// (we wait for this time twice,
--- a/jdk/src/share/classes/sun/awt/SunToolkit.java Mon Dec 19 16:44:36 2011 +0400
+++ b/jdk/src/share/classes/sun/awt/SunToolkit.java Tue Dec 20 12:48:01 2011 +0300
@@ -101,30 +101,28 @@
*/
public final static int MAX_BUTTONS_SUPPORTED = 20;
- public SunToolkit() {
- Runnable initEQ = new Runnable() {
- public void run () {
- EventQueue eventQueue;
+ private static void initEQ(AppContext appContext) {
+ EventQueue eventQueue;
- String eqName = System.getProperty("AWT.EventQueueClass",
- "java.awt.EventQueue");
+ String eqName = System.getProperty("AWT.EventQueueClass",
+ "java.awt.EventQueue");
- try {
- eventQueue = (EventQueue)Class.forName(eqName).newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println("Failed loading " + eqName + ": " + e);
- eventQueue = new EventQueue();
- }
- AppContext appContext = AppContext.getAppContext();
- appContext.put(AppContext.EVENT_QUEUE_KEY, eventQueue);
+ try {
+ eventQueue = (EventQueue)Class.forName(eqName).newInstance();
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.err.println("Failed loading " + eqName + ": " + e);
+ eventQueue = new EventQueue();
+ }
+ appContext.put(AppContext.EVENT_QUEUE_KEY, eventQueue);
- PostEventQueue postEventQueue = new PostEventQueue(eventQueue);
- appContext.put(POST_EVENT_QUEUE_KEY, postEventQueue);
- }
- };
+ PostEventQueue postEventQueue = new PostEventQueue(eventQueue);
+ appContext.put(POST_EVENT_QUEUE_KEY, postEventQueue);
+ }
- initEQ.run();
+ public SunToolkit() {
+ // 7122796: Always create an EQ for the main AppContext
+ initEQ(AppContext.getMainAppContext());
}
public boolean useBufferPerWindow() {
@@ -289,19 +287,7 @@
// return correct values
AppContext appContext = new AppContext(threadGroup);
- EventQueue eventQueue;
- String eqName = System.getProperty("AWT.EventQueueClass",
- "java.awt.EventQueue");
- try {
- eventQueue = (EventQueue)Class.forName(eqName).newInstance();
- } catch (Exception e) {
- System.err.println("Failed loading " + eqName + ": " + e);
- eventQueue = new EventQueue();
- }
- appContext.put(AppContext.EVENT_QUEUE_KEY, eventQueue);
-
- PostEventQueue postEventQueue = new PostEventQueue(eventQueue);
- appContext.put(POST_EVENT_QUEUE_KEY, postEventQueue);
+ initEQ(appContext);
return appContext;
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/EventQueue/MainAppContext/MainAppContext.java Tue Dec 20 12:48:01 2011 +0300
@@ -0,0 +1,36 @@
+/*
+ * @test
+ * @bug 7122796
+ * @summary Tests 7122796
+ * @author anthony.petrov@oracle.com
+ */
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import sun.awt.*;
+
+public class MainAppContext {
+
+ public static void main(String[] args) {
+ ThreadGroup secondGroup = new ThreadGroup("test");
+ new Thread(secondGroup, new Runnable() {
+ public void run() {
+ SunToolkit.createNewAppContext();
+ test(true);
+ }
+ }).start();
+
+ // Sleep on the main thread so that the AWT Toolkit is initialized
+ // in a user AppContext first
+ try { Thread.sleep(2000); } catch (Exception e) {}
+
+ test(false);
+ }
+
+ private static void test(boolean userAppContext) {
+ if (Toolkit.getDefaultToolkit().getSystemEventQueue() == null) {
+ throw new RuntimeException("No EventQueue for the current app context! userAppContext: " + userAppContext);
+ }
+ }
+}