This discussion is archived
11 Replies Latest reply: Sep 8, 2013 1:17 PM by WilliFirulais RSS

Strange hang in Frame

878730 Newbie
Currently Being Moderated
Hello

I have a problem with my program that will stall windows shutdown on some machines.
If the program is minimized it doesn't hang.

Compiled and run with latest java (1.6.0_b26)

EDIT: my first tests were not done correctly I have this program hanging on both XP and Win7 (when the frame is shown)

EDIT: minimal example that exhibits this problem (notice there's no call to addShutdownHook()):
public class ShutdownTest {
    public static void main(String[] args) {
        Runnable createGUI =new Runnable () {
            public void run() {
                final JFrame frame = new JFrame("Frame");
                final JTextArea comp = new JTextArea
                ("Try to restart the machine without closing or minimizing this window", 5, 50);
                frame.getContentPane().add(comp);
                frame.setLocation(300, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(createGUI);
    }
}
Original example: In order to find the problem I've made a little test program that also exhibits this problem:
import javax.swing.*;
import java.awt.*;

public class ShutdownTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Frame");
        final JTextArea comp = new JTextArea("Try to restart the machine without closing or minimizing this window");
        frame.getContentPane().add(comp);
        frame.setLocation(300, 200);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Runtime.getRuntime().addShutdownHook(new ThreadLogger());
    }


    private static class ThreadLogger extends Thread {
        @Override
        public void run() {
            Thread t = new Thread() {
                @Override
                public void run() {
                    System.out.println("Thread logger started");
                    int numThreads;
                    do {
                        numThreads = 0;

                        try {
                            System.out.println("Next AWT event:" + Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent());
                            for (Thread thread : Thread.getAllStackTraces().keySet()) {
                                // daemon threads will not prevent shutdown
                                if (!thread.isDaemon()) {
                                    numThreads++;
                                    System.err.println("Stalled thread (" + numThreads + ") : " + thread + ". State: " + thread.getState());
                               }
                            }
                            Thread.sleep(2000);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } while (numThreads > 0);
                }
            };
            t.setDaemon(true);
            t.start();
            System.out.println("Exiting shutdown hook");
        }
    }
}
After cancelling shutdown the following output is shown:
Exiting shutdown hook
Thread logger started
Next AWT event:java.awt.SentEvent[] on sun.awt.windows.WToolkit@c1b531
Stalled thread (1) : Thread[AWT-EventQueue-0,6,main]. State: WAITING
Stalled thread (2) : Thread[DestroyJavaVM,5,main]. State: RUNNABLE
Stalled thread (3) : Thread[AWT-Shutdown,5,main]. State: WAITING
Next AWT event:java.awt.event.InvocationEvent[INVOCATION_DEFAULT,runnable=javax.swing.Timer$DoPostEvent@1621e42,notifier
=null,catchExceptions=false,when=1310293751906] on sun.awt.windows.WToolkit@c1b531
Stalled thread (1) : Thread[AWT-EventQueue-0,6,main]. State: RUNNABLE
Stalled thread (2) : Thread[DestroyJavaVM,5,main]. State: RUNNABLE
Stalled thread (3) : Thread[AWT-Shutdown,5,main]. State: WAITING
Next AWT event:java.awt.event.InvocationEvent[INVOCATION_DEFAULT,runnable=javax.swing.Timer$DoPostEvent@1621e42,notifier
=null,catchExceptions=false,when=1310293751906] on sun.awt.windows.WToolkit@c1b531
Stalled thread (1) : Thread[AWT-EventQueue-0,6,main]. State: RUNNABLE
Stalled thread (2) : Thread[DestroyJavaVM,5,main]. State: RUNNABLE
Stalled thread (3) : Thread[AWT-Shutdown,5,main]. State: WAITING
Next AWT event:java.awt.event.InvocationEvent[INVOCATION_DEFAULT,runnable=javax.swing.Timer$DoPostEvent@1621e42,notifier
=null,catchExceptions=false,when=1310293751906] on sun.awt.windows.WToolkit@c1b531
Stalled thread (1) : Thread[AWT-EventQueue-0,6,main]. State: RUNNABLE
Stalled thread (2) : Thread[DestroyJavaVM,5,main]. State: RUNNABLE
Stalled thread (3) : Thread[AWT-Shutdown,5,main]. State: WAITING

(this continues indefinitely)
Stack trace:
2011-07-10 11:29:26
Full thread dump Java HotSpot(TM) Client VM (20.1-b02 mixed mode, sharing):

"Thread-4" daemon prio=6 tid=0x04573400 nid=0x7d0 waiting on condition [0x043ef000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at ShutdownTest$ThreadLogger$1.run(ShutdownTest.java:46)

"Thread-1" daemon prio=6 tid=0x04570800 nid=0xdf0 runnable [0x0434f000]
   java.lang.Thread.State: RUNNABLE
        at sun.awt.windows.WToolkit.shutdown(Native Method)
        at sun.awt.windows.WToolkit.access$200(Unknown Source)
        at sun.awt.windows.WToolkit$2$1.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"SIGTERM handler" daemon prio=10 tid=0x0456cc00 nid=0x50c in Object.wait() [0x0425f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x23782888> (a java.lang.Thread)
        at java.lang.Thread.join(Unknown Source)
        - locked <0x23782888> (a java.lang.Thread)
        at java.lang.Thread.join(Unknown Source)
        at java.lang.ApplicationShutdownHooks.runHooks(Unknown Source)
        at java.lang.ApplicationShutdownHooks$1.run(Unknown Source)
        at java.lang.Shutdown.runHooks(Unknown Source)
        at java.lang.Shutdown.sequence(Unknown Source)
        at java.lang.Shutdown.exit(Unknown Source)
        - locked <0x3873b6c0> (a java.lang.Class for java.lang.Shutdown)
        at java.lang.Terminator$1.handle(Unknown Source)
        at sun.misc.Signal$1.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"TimerQueue" daemon prio=6 tid=0x0456a000 nid=0x7e8 in Object.wait() [0x0484f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x237fd038> (a javax.swing.TimerQueue)
        at javax.swing.TimerQueue.run(Unknown Source)
        - locked <0x237fd038> (a javax.swing.TimerQueue)
        at java.lang.Thread.run(Unknown Source)

"DestroyJavaVM" prio=6 tid=0x004a9000 nid=0x7b0 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"AWT-EventQueue-0" prio=6 tid=0x0455d000 nid=0x90c runnable [0x0410e000]
   java.lang.Thread.State: RUNNABLE
        at sun.awt.windows.WInputMethod.getNativeLocale(Native Method)
        at sun.awt.windows.WInputMethod.getLocale(Unknown Source)
        at sun.awt.windows.WInputMethod.deactivate(Unknown Source)
        at sun.awt.im.InputContext.deactivateInputMethod(Unknown Source)
        at sun.awt.im.InputContext.focusLost(Unknown Source)
        - locked <0x237fc058> (a sun.awt.im.InputMethodContext)
        - locked <0x2374e190> (a java.awt.Component$AWTTreeLock)
        at sun.awt.im.InputContext.dispatchEvent(Unknown Source)
        at sun.awt.im.InputMethodContext.dispatchEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.SequencedEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.awt.EventQueue$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

"AWT-Windows" daemon prio=6 tid=0x01a51c00 nid=0xc30 runnable [0x040bf000]
   java.lang.Thread.State: RUNNABLE
        at sun.awt.windows.WToolkit.eventLoop(Native Method)
        at sun.awt.windows.WToolkit.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"AWT-Shutdown" prio=6 tid=0x01a51400 nid=0x5e0 in Object.wait() [0x03fff000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x2375a1e8> (a java.lang.Object)
        at java.lang.Object.wait(Object.java:485)
        at sun.awt.AWTAutoShutdown.run(Unknown Source)
        - locked <0x2375a1e8> (a java.lang.Object)
        at java.lang.Thread.run(Unknown Source)

"Java2D Disposer" daemon prio=10 tid=0x01a4ec00 nid=0x4b0 in Object.wait() [0x03f8f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x23762da0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x23762da0> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        at sun.java2d.Disposer.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

"Low Memory Detector" daemon prio=6 tid=0x019f2c00 nid=0x9d8 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread0" daemon prio=10 tid=0x019e7400 nid=0xe58 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x019e6400 nid=0x114 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x019e5400 nid=0xd38 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x019e0800 nid=0xb40 in Object.wait() [0x03ccf000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x23711148> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        - locked <0x23711148> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(Unknown Source)
        at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

"Reference Handler" daemon prio=10 tid=0x019dbc00 nid=0xb64 in Object.wait() [0x03c7f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x23711048> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:485)
        at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
        - locked <0x23711048> (a java.lang.ref.Reference$Lock)

"VM Thread" prio=10 tid=0x0199f800 nid=0x7c0 runnable

"VM Periodic Task Thread" prio=10 tid=0x01a0e000 nid=0x820 waiting on condition

JNI global references: 1155

Heap
 def new generation   total 4928K, used 1873K [0x23710000, 0x23c60000, 0x28c60000)
  eden space 4416K,  42% used [0x23710000, 0x238e4680, 0x23b60000)
  from space 512K,   0% used [0x23b60000, 0x23b60000, 0x23be0000)
  to   space 512K,   0% used [0x23be0000, 0x23be0000, 0x23c60000)
 tenured generation   total 10944K, used 0K [0x28c60000, 0x29710000, 0x33710000)
   the space 10944K,   0% used [0x28c60000, 0x28c60000, 0x28c60200, 0x29710000)
 compacting perm gen  total 12288K, used 170K [0x33710000, 0x34310000, 0x37710000)
   the space 12288K,   1% used [0x33710000, 0x3373a9e0, 0x3373aa00, 0x34310000)
    ro space 10240K,  51% used [0x37710000, 0x37c3dff8, 0x37c3e000, 0x38110000)
    rw space 12288K,  55% used [0x38110000, 0x387ac208, 0x387ac400, 0x38d10000)
Edited severeal times to cleanup the post

Edited by: user13397841 on 2011-07-28 04:42

Edited by: Skylla on 2011-08-29 07:43
  • 1. Re: Strange hang in Frame
    793415 Pro
    Currently Being Moderated
    <ul>
    <li>A shutdown <tt>Thread</tt> is supposed to complete quickly, so adding <tt>Thread.sleep(n)</tt> seems wrong.
    <li>Are you certain that should be a daemon <tt>Thread</tt>?
    <li>Make sure all GUI creation and update occurs on the EDT.
    </ul>
  • 2. Re: Strange hang in Frame
    878730 Newbie
    Currently Being Moderated
    First I want to say that downgrading to 1.6.0_b24 solved the problem. (1.6.0_b25 doesn't work either)

    I've found that jconsole.exe have the same problems as my example (also solved by downgrading to 1.6.0_b24)

    1) Actually the shutdown thread does return. The sleep is in a new thread started by the shutdown thread. (also the problem does not go away if there's no call to addshutdownhook(...) )

    2) again, you can leave out the shutdownhoook stuff from my example (and hence the setDaemon(true)) the problem is still there.

    3) you mean like below? Problem is still there (if jdk > 1.6.0_b24)

    Simplified example:
    public class ShutdownTest {
        public static void main(String[] args) {
            Runnable createGUI =new Runnable () {
                public void run() {
                    final JFrame frame = new JFrame("Frame");
                    final JTextArea comp = new JTextArea("Try to restart the machine without closing or minimizing this window");
                    frame.getContentPane().add(comp);
                    frame.setLocation(300, 200);
                    frame.pack();
                    frame.setVisible(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            };
            SwingUtilities.invokeLater(createGUI);
        }
    }
    If the program is minimized. everything works.
    If the window is closed, the program exits fine.

    If I select start->shutdown win7 will not shutdown because the program is not responding.
    If i then cancel the program has become unresponsive and taskmananger shows 50% usage by java.exe (i have dual core)

    The above is also true for jconsole.exe (which comes with the sdk)

    This must be a Java bug, right?

    (only i would like if somebody could confirm before i report a bug)

    also i would like to correct my self in the first post. The 64bit win7 wasn't running 1.6.0_b26 but b24. (and i'm not sure about the win32 machine that did work at first, but is also hanging now)
    Edited by: user13397841 on 2011-07-28 08:09

    Edited by: user13397841 on 2011-07-28 08:12
  • 3. Re: Strange hang in Frame
    mKorbel Explorer
    Currently Being Moderated
    public class ShutdownTest {
        public static void main(String[] args) {
            Runnable createGUI =new Runnable () {
                public void run() {
                    final JFrame frame = new JFrame("Frame");
                    final JTextArea comp = new JTextArea
                    ("Try to restart the machine without closing or minimizing this window", 5, 50);
                    frame.getContentPane().add(comp);
                    frame.setLocation(300, 200);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(createGUI);
        }
    }
    Edited by: mKorbel on 28.7.2011 22:56

    Edited by: mKorbel on 28.7.2011 22:56
  • 4. Re: Strange hang in Frame
    878730 Newbie
    Currently Being Moderated
    Except for the bigger JtextArea (and readability on this forum), I don't see any difference
  • 5. Re: Strange hang in Frame
    EJP Guru
    Currently Being Moderated
    A shutdown hook should certainly not be starting new threads.
  • 6. Re: Strange hang in Frame
    878730 Newbie
    Currently Being Moderated
    I only added the shutdown hook to see what non-daemon threads was still running.

    Even if I don't add any shutdownhook (see latest example) Java still hangs after I choose to shutdown windows (with full cpu usage).
  • 7. Re: Strange hang in Frame
    878730 Newbie
    Currently Being Moderated
    would any one care to confirm?

    You can try with jdk 1.6.0_b25 or jdk_1.6.0_b26 (both should hang)

    either with my simple example above or with jconsole.exe (i suppose it's the same problem bug but don't know)

    start jconsole.exe (e.g. from c:\program files\java\jdk1.6.0_26\bin\ )

    when jconsole have started try to reboot or shutting down windows (be sure not to minimize jconsole)

    What you should see is that jconsole will not close. (as with the example that i've posted)

    The result for me is that:
    with 1.6.0_b24 there is no problem
    with 1.7.0 there is no problem

    only with 1.6.0_b25 and jdk_1.6.0_b26 i can reproduce the hang
  • 8. Re: Strange hang in Frame
    884311 Newbie
    Currently Being Moderated
    One of our users has seen this problem with 1.6.0_26, too. Reverting back to 1.6.0_24 solved the problem.
  • 9. Re: Strange hang in Frame
    jduprez Pro
    Currently Being Moderated
    From the Javadoc of Runtime.addShutdownHook (highlighted mine);
    +Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.+

    Edited by: jduprez on Aug 24, 2011 11:59 PM
    Oops, I realized this is not helping you, I should have read all replies first!
  • 10. Re: Strange hang in Frame
    878730 Newbie
    Currently Being Moderated
    with java 6 update 27 application still hangs.
    jconsole also hangs if visible during shutdown.
  • 11. Re: Strange hang in Frame
    WilliFirulais Newbie
    Currently Being Moderated

    Bug still available in java version "1.7.0_21" on Windows 64 bit. Seems noone is realy interrested in ;-(

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points