⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
a native <CODE>MFC</CODE> call that does not return. That is
where the real problem lies and is a bug in the Java core classes. 
The user's code was okay.
<P>
<b>Example 2</b>
<P>
In this second example you will investigate a bug that on initial
outset appears to be a fault in Swing but as you will discover is due to 
the fact that Swing is not thread safe.
<p>
Again the bug report is available to view on the JDC site, the bug number this 
time is 
<A HREF="/developer/bugParade/bugs/4098525.html">4098525</A>.
<P>
Here is a cut down sample of the code used to reproduce this problem. The modal dialog is being created from within the <CODE>JPanel paint</CODE> method. 
<P>
</FONT>

<PRE>
<FONT SIZE=-1>
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;

class MyDialog extends Dialog 
                         implements ActionListener {

    MyDialog(Frame parent) {
        super(parent, "My Dialog", true); 
        Button okButton = new Button("OK");
        okButton.addActionListener(this);
        add(okButton);
        pack();
    }

    public void actionPerformed(ActionEvent event) {
         dispose();
    }
}

public class Tester extends JPanel {

    MyDialog myDialog;
    boolean firstTime = true;

    public Tester (JFrame frame) throws Exception {
        super();
	myDialog = new MyDialog(frame);
    }

    void showDialogs() {
        myDialog.show();
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (firstTime) {
           firstTime = false;
           showDialogs();
        }
    }

    public static void main(String args[]) 
                              throws Exception {

       JFrame frame = new JFrame ("Test");
       Tester gui = new Tester(frame);
       frame.getContentPane().add(gui);
       frame.setSize(800, 600);
       frame.pack();
       frame.setVisible(true);
    }
}
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<p>
When you run this program you find that it deadlocks straight away. By taking
a stack trace you see the <A HREF="trace2.trc">these key threads</A>.

<P>
The stack trace you have here is slightly different to the stack trace that 
appears in the bug report, but caused by the same effect. We are also
using the Java 2 release to generate the trace and supplied the option
<CODE>-Djava.compiler=NONE</CODE> when you ran the program so that you
could see the source line numbers. The thread to look for is the thread
in MW, monitor wait which in this case is thread <CODE>AWT-EventQueue-1</CODE>

</FONT>

<PRE><FONT SIZE="-1">
"AWT-EventQueue-1" (
       TID:0xebca8c20, sys_thread_t:0x376660, 
				state:MW) prio=6
 at java.awt.Component.invalidate(Component.java:1664)
 at java.awt.Container.invalidate(Container.java:507)
 t java.awt.Window.dispatchEventImpl(Window.java:696)
 at java.awt.Component.dispatchEvent(
                          Component.java:2289)
 at java.awt.EventQueue.dispatchEvent(
                          EventQueue.java:258)
 at java.awt.EventDispatchThread.run(
                          EventDispatchThread.java:68)
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

If you look for that line in file <CODE>java/awt/Component.java</CODE> which is contained in the <CODE>src.jar</CODE> archive, you see the following:
</FONT>

<PRE>
    public void invalidate() {
        synchronized (getTreeLock()) { //line 1664
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
This is where our application is stuck, it is waiting for the <CODE>getTreeLock</CODE> monitor lock to become free. The next task is to find out which thread has this <CODE>getTreeLock</CODE> monitor lock held.
<p>
To see who is holding this monitor lock you look at the Monitor cache dump and
in this example you can see the following:

</FONT>

<PRE><FONT SIZE="-1">
Monitor Cache Dump:
  java.awt.Component$AWTTreeLock@EBC9C228/EBCF2408: 
	owner "AWT-EventQueue-0" ( 0x263850) 3 entries
  Waiting to enter:
    "AWT-EventQueue-1" (0x376660)
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<p>
The method <CODE>getTreeLock</CODE> monitor is actually a lock on a 
specially created inner class object of<CODE>AWTTreeLock</CODE>. This is 
the code used to create that lock in file <CODE>Component.java</CODE>.
</FONT>

<PRE>
    static final Object LOCK = new AWTTreeLock();
    static class AWTTreeLock {}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
The current owner is <b>AWT-EventQueue-0</b>. Thie thread called our <CODE>paint</CODE> method to create our modal <CODE>Dialog</CODE> via a call to <CODE>paintComponent</CODE>. <CODE>paintComponent</CODE> itself was called from an <CODE>update</CODE> call of <CODE>JFrame</CODE>.
<p>
So where was the lock set? Well there is no simple way to find out which
stack frame actually held the lock but on a simple search of <CODE>javax.swing.JComponent</CODE> you see that <CODE>getTreeLock</CODE> is called inside
the method <CODE>paintChildren</CODE> which you left at line 388.

</FONT>

<PRE>
at Tester.paint(Tester.java:39)
at javax.swing.JComponent.paintChildren(
                            JComponent.java:388)
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

The rest of the puzzle is pieced together by analyzing the <CODE>MDialogPeer show</CODE> method. The Dialog code creates a new ModalThread which is why
you see an <CODE>AWT-Modal</CODE> thread in the stack trace output, this
thread is used to post the Dialog. It is when this event is dispatched
using <CODE>AWT-EventQueue-1</CODE> which used to be the AWT Dispatch proxy
that <CODE>getTreeLock</CODE> monitor access is required and so you have a deadlock.
<p>
Unfortunately Swing code is not designed to be thread safe and so the
workaround in this example is to not create modal dialogs inside a Swing
paint methods. Since Swing has to do alot of locking and calculations as
to which parts of a lightweight component needs to be painted it is strongly
advised to not include sychronized code or code that will result
in a synchronized call such as in a modal dialog, inside <CODE>paint</CODE> method.
<p>
This completes Java stack traces theory, and you should now know
what to look for the next time you see a stack trace. To save time, 
you should make full use of the JDC bug search to see if the problem you 
are having has already been reported by someone else.

<A NAME="checklist"></A>
<H3>Expert's Checklist</H3>

To summarize, these are the steps to take the next time you
come across a problem in a Java program.

<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<STRONG>Hanging, deadlocked or frozen programs:</STRONG> If you think 
your program is hanging, generate a stack trace. Examine the threads in states 
<CODE>MW</CODE> or <CODE>CW</CODE>. If the program is deadlocked, some of 
the system threads will probably show up as the current thread because there 
is nothing else for the Java VM to do.</FONT>

<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<STRONG>Crashed or aborted programs:</STRONG> On UNIX look for a core file. You
can analyze this file in a native debugging tool such as 
<CODE>gdb</CODE> or <CODE>dbx</CODE>. Look for threads that have called 
native methods. Because Java technology uses a safe memory model, any 
corruption probably occurred
in the native code. Remember that the Java VM also uses native code so it 
might not be a bug in your application.</FONT>

<P>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<STRONG>Busy programs:</STRONG> The best course of action you can take for busy
programs is to generate frequent stack traces. This will narrow down the
code path that is causing the errors, and you can start your investigation
from there.</FONT>
</UL>


<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>

<P>
_______<BR>
<A NAME="TJVM"><SUP>1</SUP></A> As used on this web site, 
the terms &quot;Java virtual 
machine&quot; or &quot;JVM&quot; mean a virtual machine 
for the Java platform.

</FONT>
</TD>
</TR>
</TABLE>




<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="TOP">
	
    <P ALIGN=CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    <A HREF="http://java.sun.com/jobs/">Employment</A> - 
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <a href="/siteinfo/faq.html">FAQ</a> |
    <a href="/feedback/index.html">Feedback</a> | 
    <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-99
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -