java - Why should SwingUtils.invokeAndWait() method be called outside an EDT thread? -


edit : have referred link , i'm able understand codeflow of invokelater. question is, why logic implemented way? there specific reasons?


following code:

  private void init()      {     jframe jfr = new jframe();     jfr.setsize(500, 500);     jfr.setvisible(true);     jfr.settitle("test");       jbutton jb = new jbutton("ok");     jfr.add(jb);      jb.addactionlistener(new actionlistener()     {         @override         public void actionperformed(actionevent e)         {             try              {                 swingutilities.invokeandwait(new runnable()                  {                     @override                     public void run()                      {                         system.out.println("hello");                     }                 });             }              catch (exception e1)              {                 e1.printstacktrace();             }          }     }); 

first question (while using invokeandwait):

why implemented in such way throws invocationtargetexception when called within edt thread?

second question (while using invokelater):

why invokelater allows ?

well, basic understanding on edt threads:

invokeandwait:

  • places job f edt event queue , waits until edt has executed it.
  • this call blocks until pending awt events(a, b, c, d, e) have been processed , (then) execute f after control returns.
  • returns if , if job submitted completed.(control returns after f completed.)
  • technically, synchronous blocking call.

invokelater:

  • places job f edt queue , doesn't wait completion.(basically , publish , return call).
  • if don't care job completion can use invokelater.
  • technically, asynchronous non-blocking call.

edt same awt. ui events in awt scheduled on single thread called edt. thread used process ui related events in swing.

invokeandwait: invoke task on same thread , wait task completed. result in deadlock. method never return cuz waiting task, task never run because method never completed.

invokelate: works because not waiting task completed. invoke in exact same thread, not waiting complete, won't result in deadlock.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -