views:

72

answers:

1

Hi all,

I'm developing an applet that makes some work and then a redirection to an URL when user clicks on a button. I'm using a SwingWorker to prevent GUI gets locked up.

But when I run the applet I get this error after clicking the button:

Exception in thread "SwingWorker-pool-1-thread-2" java.lang.IllegalMonitorStateException at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source) at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source) at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source) at java.util.concurrent.LinkedBlockingQueue.poll(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

It looks like the applet is destroying itself but the worker thread is still alive..If I add a Thread.sleep(2000); as a test, the applet runs right, but I think it's not the right way to fix it.

What I'm doing wrong?? Maybe I should make the redirection from the main thread? Maybe destroy method of the applet should wait to worker thread to finish?

Java source:

public class MyApplet extends javax.swing.JApplet {
    class MyWorker extends SwingWorker<Boolean , Void> {
        protected Boolean doInBackground() throws Exception {
  // DO THE HARD JOB...
        }

        public void done() {
  netscape.javascript.JSObject.getWindow(this).call("onDone", new String[] { "" });
        }
    }

    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
             initComponents();
                    setBackground(Color.WHITE);
                }
            });
        } catch (Exception ex) { 
  ex.printStackTrace();
 }
   }

   private void jMyButtonctionPerformed(java.awt.event.ActionEvent evt) {                                              
        new MyWorker().execute();        
    }                                             
}

HTML source:

<script type="text/javascript">
function onDone(){document.location.href="http://myurl.com";}
</script>
A: 

Finally solved using SwingUtilities.invokeLater :D

Henry
Oops, I'm afraid but it still crashes
Henry