java - Can I run two EDTs? -


this theoretical question. example below made me think of it, may not best example. please assume reason's below concrete, , can't moment worked around.

the program have been running has optional debug frame created on program startup, , made visible user pressing buttons/keyboard shortcut.

my issue have lengthy processes on edt, if hanging reason or fails i'd see straight away, , not wait thread end running, update debug log frame.

my solution have 2 separate edt 2 separate guis updated separate thread.

is possible, or not able so? haven't found online resourcethat show me how to. know edts should single threaded, if keep threads disentangled, can have two? please?

the answer simple: no cannot have 2 edts, not possible. not stuck frozen gui, have options available.

first , foremost, 2 important rules:

  1. never lengthy calculations in edt. ever.
  2. never manipulate swing components outside edt. ever.

breaking first rule result in gui being frozen , no events of sort being processed during time. includes updates gui want during calculations not appear until after calculations done.

the latter ignored go unnoticed of time, can - , - bite in ass , of time huge pita go , fix it. correct way start. can happen? components can display in broken state, may appear white, or whole application can freeze because there deadlock between edt , other threads (been there, done that). adhere oracle swing threading policy!


so, how avoid doing lengthy calculations on edt after example user pressed button? options:

  • use swingworker. advantage: has done() method can use automatically executed in edt once worker done. implements future interface , can therefore used return result.
  • you can create own runnable , calculations in there.
  • use other way java provides parallel execution.

ok, , how avoid ever manipulating gui outside edt?

  • call swingutilities.invokelater() , execute swing manipulations in there.
  • use swingworkers done() method described above.

if follow these 2 rules, swing gui in better shape , can focus more on application development rather swing issues.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -