Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: intermittent hang on launch



> That shouldn't be necessary. You are explicitly allowed to call Swing
 methods from any thread until the component is realized. The problem
 is thread safety and calls to paint() from the event loop, but the
 component won't be painted until it's shown.

Yes and no. For example, Containers are not synchronized, so adding/
removing Components from multiple threads has concurrency issues with accessing the underlying collection. As for the code fragment I posted, realization occurs during pack(), so pack() and show() must be done on the Swing thread. Constructing the object could be done in-
line in main first:


     public static void main (String[] args) {
         final MyMainFrame frame = new MyMainFrame();

         SwingUtilities.invokeLater (new Runnable() {
             public void run() {
                 frame.pack();
                 frame.show();
             }
         });
     }

Not that it's worth getting overly pedantic over such a trivial point, but constructing, packing and showing my main frame from within main() is a pattern I have followed for years and years and years without problem. Indeed, it's explicitly declared to be safe by Sun "An application's GUI can often be constructed and shown in the main thread", see http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html


The only caveat is that this has to be the FIRST bit of GUI code in your application. After you have created and shown any components and the event loop comes into existence then it's no longer safe. But if all you're doing is creating and showing a single frame then it's perfectly safe.

-Rolf
--
Rolf Howarth
Square Box Systems Ltd
Stratford-upon-Avon UK.
http://www.squarebox.co.uk
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.