java - Synchronizing on a passed object, JFrame is freezing -
i created jframe contains , internalframe draws figures moving(each figure thread) wanted pausebuttonmake pause, wanted synchronize them on passed object.
but when hit pause button whole window freezing , cannot hit play button thing 1 running @ time, want them running pause.
class extends jframe{ .... object o = new object(); jbuttton pausebutton = new jbutton("pause"); jbuttton playbutton = new jbutton("play"); b b = new b(o); pausebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { synchronized (synchronizator) { try { synchronizator.wait(); } catch (interruptedexception e) { e.printstacktrace(); } } } }); playbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { synchronized (synchronizator) { synchronizator.notifyall(); } } ... } class b extends jinternalframe{ object o; b(object o){this.o = o} ./... many... c thread = new c(o); .... } class c extends thread{ object o; booolean running; public void run(){ while(running){ synchronized(o){ } } } }
noooo!! ;)
all swing activity should done on awt event dispatch thread (edt). use normal thread-agnostic objects , perhaps javax.swing.timer (not java.util!) timing.
you may want other things not involving swing on different threads, suggest keeping clean separation. few objects should dealing thread issues.
if using bare low-level java synchronisation facilities, set condition prior notify/notifyall , put waits within while loops.
Comments
Post a Comment