java - Why won't this code deadlock? -


i'm investigating deadlock problem on settext need first learn , understand deadlocks. end have created short program try , replicate may happening on larger scale unsure why smaller program never deadlocks.

here learning program:

public static void main(string[] a) {     jframe frame = new jframe();     final jtextfield p = new jtextfield("start");      jbutton btn = new jbutton("button");     btn.addactionlistener(new actionlistener()     {         @override         public void actionperformed(actionevent e)         {             swingutilities.invokelater(new runnable(){                 @override                 public void run(){                     p.settext(string.valueof(system.nanotime()));                 }             });         }     });      frame.getcontentpane().setlayout(new flowlayout());     frame.getcontentpane().add(p);     frame.getcontentpane().add(btn);     frame.setsize(400, 400);     frame.setdefaultcloseoperation(frame.exit_on_close);     frame.setvisible(true); } 

i thought modifications swing not done in separate thread, have settext change jtextfield on button click in invokelater. doing should break single thread rule, not cause deadlock?

making changes swing components other threads won't deadlock program (at least not usually)--it's jvm isn't obligated reflect changes state made in 1 thread in other threads unless there's happens-before relationship, such synchronized block or access volatile field. jvm might decide read variable's value once , never reread in current thread, mean update never seen thread that's drawing ui, or might around updating @ unpredictable later time.

using invokelater insert update edt ensures there's happens-before between settext , next draw operation.

update: you've succeeded in making code deadlock moving queue runnable, problem edt isn't running yet when try queue operation on it.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -