Friday, 15 August 2014

java - How to wake up waiting SwingWorker? -


I have swingwalker that is running in the background after working on some stuff Will wait when a user presses a button, then MyListener is applied and the worker is awakened to continue some stuff. It is not working as if I thought it would be. It throws an exception below, please tell me what I have done.

  Work worker = new MyWorror (); The square applies MyListener ActionListener {@ Override Public Zero Action Action (Action Avenue) (do some things and awake the worker worker. Notify (); }} Class MyWorker Expands SwingWorker {Boolean_work = True; The @OverWide object throws doInBackground Safe (exception) {System.out.println ("in myWorker"); While (_work == true) {// some stuff here wait (); } Return tap; }}   

Exception:

  Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException at java.lang.Object.notify (javax at Native in .swing.AbstractButton $ Handler.actionPerformed worker.Worker $ 2.actionPerformed (Worker.java:54) on javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:2018) (AbstractButton.java:2341) Javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402) javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel.java Cheer 59) javax.swing.plaf.basic.BasicButtonListener.mouseReleased (BasicButtonListener.java:252) Java on .awt.Component.processMouseEvent (Component.java:6505) javax.swing.JComponent.processMouseEvent (JComponent.java:3321) on java.awt.Component.processEvent (Component.java:6270) on java.awt.Container . Java.awt.Component.dispatchEventImpl (component.java:4861) on java.awt.Container.dispatchEventImpl (Kantenrkjawa 28,287) on ProcessEvent (Kantenrkjawa 2222) mponent.dispatchEvent on java.awt.Co (Component.java: 4687) java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4832) java.awt.LightweightDispatcher.processMouseEvent (Container.java:4492) java.awt on java.awt.LightweightDispatcher.dispatchEvent (container Java: 4422). Container.dispatchEventImpl (Container.java:2273) java.awt.Window.dispatchEventImpl (Window.java:2713) on java.awt.Component.dispatchEvent (Component.java:4687) java.awt.EventQueue $ 3.run On java.awt.EventQueue.dispatchEventImpl (EventQueue.java:707) java.awt.EventQueue.access $ 000 (EventQueue.java:666) on Java at. awt.EventQueue $ 3.run java.security.ProtectionDomain java.security.AccessController.doPrivileged (Native Method java.security.ProtectionDomain $ 1.doIntersectionPrivilege at $ 1.doIntersectionPrivilege) to (ProtectionDomain.java:76) (EventQueue. Java: 664) (ProtectionDomain.java:87) java.awt.EventQueue $ 4.run (EventQueue.java:680) java.awt.EventQueue $ 4.run (Java) on Java.security Java $ .run (EventQueue.java:678) Security.ProtectionDomain $ 1.doInIntersectionPrivilege (ProtectionDomain.java:76) at Java.ev.EventKIU JavaEvent.Event.DispatchTrade on DisplaysAvent (EventQueue.java:677) .PopOneEventForFilters (AccessController.doPrivileged on EventDispatchThread.java): 211) java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:128) java.awt .EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java.17) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java113) on Java Java.awt.EventDispatchThread.run (EventDispatchThread.java:90)    

You need to add synchronized blocks around the notification call.

  Synchronize (worker) {worker.notify (); }   

Now, it is being said that, you generally get frustrated using the instance variable as the monitor lock and instead of the constant last Use lock object (among other things) This likely reduces the wrong example

No comments:

Post a Comment