开发者

Applet crashes when redirecting to a new url

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.re开发者_开发知识库lease(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>


Finally solved using SwingUtilities.invokeLater :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜