swing - Java Event-Dispatching Thread explanation -


i've started learning , exploring basics of gui programming in java.

having been programming while have done backend work or work , result closest i've gotten user interfaces command console (embarrassing know).

i'm using swing , far can gather means extension using awt.

my question based on piece of code:

java.awt.eventqueue.invokelater(new runnable() {     public void run() {         new frame.setvisible(true);     } } ); 

i have been researching while wanted understand strange piece of code , have come across term 'event-dispatching thread' multiple times. correct me if i'm wrong understand it; has using multiple threads , how java swing interprets threads. gather above code used make sure threads 'safe' before creates window, hence invokelater?

i have read that:

"you can call methods operate on frame event-dispatching thread"

and under circumstances can call methods operate on frame main method.

can please clarify me event-dispatching thread is?

how relates multiple threads of execution , how threads not safe called main method? why need invokelater?

can not create window other object?

i've hit bit of road block in research i'm not grasping these relations , ideas.

a side note base knowledge on in-depth understanding believe leads best overall outcome , result best programs. if understand in-depth how works can use tips , tweaks rather parroting them in code, please don't afraid give me in-depth explanations , broaden knowledge.

thank you.

the eventdispatching thread special thread managed awt. thread runs in infinite loop processing event. java.awt.eventqueue.invokelater method special way provide code run on event queue. writing ui framework safe in multithreading environment difficult awt authors decided allow operations on gui objects occur on single special thread. event handlers execute on thread , code modifies gui should operate on thread.

now awt not check not issuing gui commands thread (the wpf framework c# this). possible write lot of code , pretty agnostic , not run problems. can lead undefined behavior best thing ensure gui code runs on event dispatcher thread. invokelater provides mechanism this.

so classic example need run long running operation downloading file. launch thread perform action when completed use invokelater update ui. if didn't use invokelater , instead updated ui directly might have race condition , undefined behavior occur.

wikipedia has more information: http://en.wikipedia.org/wiki/event_dispatching_thread

also if curious why awt authors don't make toolkit multithreaded here article: http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -