java - When is the Swing UI thread created? -
when, in process of running swing program, ui thread (event-dispatch thread, edt) first spawned? presumably given jvm whatever wants (for example, spawning edt @ start-up, whether or not it's ever used), practical matter when edt typically created?
does created when swingutilities.invokelater() first called? when jpanel first instantiated? , if event pump started separately creating edt, when typically happen?
after looking through code, seems if it's "lazily initialized", meaning initialized needed, if not initialized. in case, whenever event posted it's queue.
here's total story:
the eventdispatchthread
encapsulated within eventqueue
. each eventqueue
has own edt:
/** * summary of class */ public class eventqueue { private static final int ultimate_priority = 3; private static final int num_priorities = ultimate_priority + 1; private queue[] queues = new queue[num_priorities]; private eventqueue nextqueue; private eventqueue previousqueue; private eventdispatchthread dispatchthread; }
the dispatchthread
initialized using package-private method initdispatchthread()
:
final void initdispatchthread() { pushpoplock.lock(); seek { if (dispatchthread == null && !threadgroup.isdestroyed() && !appcontext.isdisposed()) { dispatchthread = accesscontroller.doprivileged( new privilegedaction<eventdispatchthread>() { public eventdispatchthread run() { eventdispatchthread t = new eventdispatchthread(threadgroup, name, eventqueue.this); t.setcontextclassloader(classloader); t.setpriority(thread.norm_priority + 1); t.setdaemon(false); awtautoshutdown.getinstance().notifythreadbusy(t); homecoming t; } } ); dispatchthread.start(); } } { pushpoplock.unlock(); } }
after checking references method, there 3 places method called:
in private methodeventqueue#wakeup(boolean)
in private method eventqueue#posteventprivate(awtevent)
(which called public method eventqueue#postevent(awtevent)
) in package-private method eventqueue#createsecondaryloop(conditional, eventfilter, long)
. before initdispatchthread()
called, dispatchthread
checked create sure it's not initialized. there few ways can view entire source code class in jdk (easiest beingness attaching source); these methods if you're really interested.
so know eventqueue
contains thread, , thread created whenever it's needed (an event gets posted). time talk queue located , how things communicate it.
if check code of eventqueue#invokelater(runnable)
(which called it's swingutilities
counterpart), you'll see calls toolkit.geteventqueue().postevent(...)
. tells queue located in toolkit
.
inside toolkit
class, can see it's created (if not already) time phone call it. uses reflection create object:
public static synchronized toolkit getdefaulttoolkit() { if (toolkit == null) { seek { java.lang.compiler.disable(); java.security.accesscontroller.doprivileged( new java.security.privilegedaction<void>() { public void run() { string nm = null; class<?> cls = null; seek { nm = system.getproperty("awt.toolkit"); seek { cls = class.forname(nm); } grab (classnotfoundexception e) { classloader cl = classloader.getsystemclassloader(); if (cl != null) { seek { cls = cl.loadclass(nm); } grab (classnotfoundexception ee) { throw new awterror("toolkit not found: " + nm); } } } if (cls != null) { toolkit = (toolkit)cls.newinstance(); if (graphicsenvironment.isheadless()) { toolkit = new headlesstoolkit(toolkit); } } } grab (instantiationexception e) { throw new awterror("could not instantiate toolkit: " + nm); } grab (illegalaccessexception e) { throw new awterror("could not access toolkit: " + nm); } homecoming null; } }); loadassistivetechnologies(); } { // create sure re-enable jit. java.lang.compiler.enable(); } } homecoming toolkit; }
toolkit abstract class. instead of instantiating object of class, creating instance of subclass of toolkit: suntoolkit
. need know see queue created.
once have toolkit, can access it's eventqueue using toolkit#getsystemeventqueue()
. telescopes protected abstract method getsystemeventqueueimpl()
. must check out subclass see implementation method. in suntoolkit class, have:
protected eventqueue getsystemeventqueueimpl() { homecoming getsystemeventqueueimplpp(); } // bundle private implementation static eventqueue getsystemeventqueueimplpp() { homecoming getsystemeventqueueimplpp(appcontext.getappcontext()); } public static eventqueue getsystemeventqueueimplpp(appcontext appcontext) { eventqueue theeventqueue = (eventqueue) appcontext.get(appcontext.event_queue_key); homecoming theeventqueue; }
(eventqueue) appcontext.get(appcontext.event_queue_key)
queue coming appcontext
of toolkit. gotta find queue added app context:
public suntoolkit() { runnable initeq = new runnable() { public void run() { eventqueue eventqueue; string eqname = system.getproperty("awt.eventqueueclass", "java.awt.eventqueue"); seek { eventqueue = (eventqueue) class.forname(eqname).newinstance(); } grab (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); //queue added here posteventqueue posteventqueue = new posteventqueue(eventqueue); appcontext.put(post_event_queue_key, posteventqueue); } }; initeq.run(); }
so quick overview:
the edt located within eventqueue the eventqueue located within toolkit the queue created when create toolkit the toolkit created either manually (by callingtoolkit.getdefaulttoolkit()
, or whenever part of programme (such swing component posting info queue) calls on it) the edt created anytime event posted queue (and edt isnt running) let me know if have questions this
java swing event-dispatch-thread
No comments:
Post a Comment