|
|
||
Alexander Potochkin's BlogJanuary 2006 ArchivesDebugging Swing, the summary #1Posted by alexfromsun on January 12, 2006 at 09:38 AM | Permalink | Comments (12)Well, I've got more than 30 comments for the previous blog. It's high time to summarize them and make up a conclusion.
There are two separate enhancements were suggested in comments:
Making Swing methods threadsafe, simple caseReally, if you call Swing method out from EDT why compiler can't move it to EDT automatically ? For example we can mark necessary methods with @OnEdt annotaions and... The compiler could probably insert SwingUtilities.invokeLater automatically: From
@OnEdt
public void removeAll() {
// implementation skipped...
}
To
public void removeAll() {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
removeAll();
}
});
// implementation skipped...
}
}
No need to write InvokeLater(..) yourself anymore, compiler is doing it for us. One of the serious drawback of such solution is a performance: You'll need an additional inner class for each @OnEdt method, The second problem is not so visible, Making Swing methods threadsafe, real caseconsider the more usual case:
@OnEdt
public Component getComponent(int number) {
// implementation skipped...
return aComponent;
}
This method takes a parameter and returns a value,
The guru of Swing threading and one of the creators of SwingWorker,
if (!SwingUtilities.isEventDispatchThread()) {
store all the method arguments in final variables
Callable
To block the thread during return value calculation Looks much more scary, doesn't it ? ConclusionStandard Java doesn't support preprocessors or any other way to insert an additional code into existing method and there are some strong reasons why is it so. Peter van der Ahe, a java compiler expert expains it in very clear way: If we generate a bunch of additional code for methods marked with @OnEdt annotation we would compromise this principle. If java invoked methods on the other thread behind the scene "What you see is what you get" - the code is 100% controlled by a programmer, no tricky magic, everything is clear Hence thinking about all listed problems Check out a good Graham Hamilton's blog about "a multithreaded toolkit dream". It is becoming clear why SWT, Win32 API, Motif, Xlib and GTK Improving Swing debugging is much more realistic and promising issue, Stay tuned ! | ||
|
|