[ Pobierz całość w formacie PDF ]
.Controlling Threads of ExecutionAs you know from Chapter 15, “Threads and Multithreading,” Java applets have multiple threads of execution.Using JDB and the threads command, we can view these threads asfollows:AWT-Callback-Win32[1] threadsGroup sun.applet.AppletViewer.main: 1.(java.lang.Thread)0x13a3a00 AWT-Win32 running 2.(java.lang.Thread)0x13a2a58 AWT-Callback-Win32 running 3.(sun.awt.ScreenUpdater)0x13a2d98 Screen Updater runningGroup group applet-StartApplet.class: 4.(java.lang.Thread)0x13a28f0 class runningAWT-Callback-Win32[1]As you can see from the output, there are four threads of simultaneous applet execution.Two correspond to the AWT window management system (threads 1 and 2), one for updating the screen (thread 3), and one for the actual applet itself (thread 4).JDB provides two commands for controlling the execution of threads: suspend and resume.Suspending a thread isn't very worthwhile in our simple example, but in multithreaded applications it can be very worthwhile—you can suspend all but one thread and focus on that thread.But let's try suspend and resume on our applet to get a feel for their use.To suspend the AWT-Win32 thread, you should note its ID from the threads list and then use this as the argument to suspend, as follows:AWT-Callback-Win32[1] threadsGroup sun.applet.AppletViewer.main: 1.(java.lang.Thread)0x13a3a00 AWT-Win32 running.AWT-Callback-Win32[1] suspend 1AWT-Callback-Win32[1] threadsGroup sun.applet.AppletViewer.main: 1.(java.lang.Thread)0x13a3a00 AWT-Win32 suspended 2.(java.lang.Thread)0x13a2a58 AWT-Callback-Win32 running 3.(sun.awt.ScreenUpdater)0x13a2d98 Screen Updater runningGroup group applet-StartApplet.class: 4.(java.lang.Thread)0x13a28f0 class runningAWT-Callback-Win32[1]As expected, the AWT-Win32 thread is now suspended.Threads are resumed in a completely analogous manner as follows:AWT-Callback-Win32[1] resume 1AWT-Callback-Win32[1] threadsGroup sun.applet.AppletViewer.main: 1.(java.lang.Thread)0x13a3a00 AWT-Win32 running 2.(java.lang.Thread)0x13a2a58 AWT-Callback-Win32 running 3.(sun.awt.ScreenUpdater)0x13a2d98 Screen Updater runningGroup group applet-StartApplet.class: 4.(java.lang.Thread)0x13a28f0 class runningAWT-Callback-Win32[1]Using use to Point the Way to Your Java Source CodeIn order to execute the list command, JDB takes the line number and grabs the required lines of Java from the source file.To find that source file, JDB reads your CLASSPATH environmental variable and searches all of the paths contained in it.If that path doesn't contain your source file, JDB will be unable to display the source for your program.This wasn't a problem for us because our search path contained the current directory, but if you set up your applet or application and the source is located in a directory outside of the search path, you'll need to use the use command to add to your path.The use command without any arguments displays the current search path as follows:AWT-Callback-Win32[1] use\java\classes;.;C:\JAVA\BIN\.\classes;AWT-Callback-Win32[1]Appending a directory to the search path is unfortunately slightly tedious.You have to retype the entire current path and add the new path.So to add the path \myclasses to the preceding path, we would do the following:AWT-Callback-Win32[1] use \java\classes;.;C:\JAVA\BIN\.\classes;\myclassesAWT-Callback-Win32[1]Getting More Information About Your Objects with dumpIn our debugging section we had an example of how to display an object's value using the print command.In this section, we'll look at JDB's dump command, which is a more useful display command for objects containing multiple data members.The AddNumbers class is a good example (note that this in this case refers to the instantiation of AddNumbers for our applet), as follows:AWT-Callback-Win32[1] dump thisthis = (AddNumbers)0x13a3000 { ComponentPeer peer = (sun.awt.win32.MFramePeer)0x13a31b0 Container parent = null int x = 0 int y = 0 int width = 300 int height = 200 Color foreground = (java.awt.Color)0x13a2bb0 Color background = (java.awt.Color)0x13a2b98 Font font = (java.awt.Font)0x13a31d0 boolean visible = true boolean enabled = true boolean valid = true int ncomponents = 3AWT-Callback-Win32[1] _ Contrast this with the output from print: AWT-Callback-Win32[1] print thisthis = AddNumbers[0,0,300x200,layout=java.awt.BorderLayout,resizable,title=JDB Sample Java Program]AWT-Callback-Win32[1]As you can see, the dump command displays the data members for the class, while print displays only the key attributes for the class.Handling Exceptions with catch and ignoreJDB has two functions for dealing with exceptions: catch and ignore.catch, similar to a breakpoint, enables you to trap exceptions and stop the debugger.This is useful when debugging because it becomes much easier to diagnose an exception when you know the conditions under which it occurred.To catch an exception, simply type class and the name of the exception class.In order to trap any exception (the Exception exception base class), the following is done:AWT-Callback-Win32[1] catch ExceptionAWT-Callback-Win32[1]ignore does exactly the opposite of catch.It squelches the specified class of exceptions raised by an applet or application.The use of ignore is completely analogous to catch, as shown by the following:AWT-Callback-Win32[1] ignore ArrayOutOfBoundsExceptionAWT-Callback-Win32[1]Continuing Program Execution with contYou may be wondering how to restart execution once you reach a breakpoint and execution has stopped.The cont command does just that:AWT-Callback-Win32[1] contAs you can see, the program has resumed execution and the JDB prompt will not return until a breakpoint or exception is reached.Leaving JDB Using the exit CommandAlthough it may be obvious to you already, there is one final command that comes in handy once in any debugging session.The exit command lets you out of the debugger and backinto DOS.SummaryIn this chapter, you have learned through hands-on experience about Sun's Java debugger, JDB.Debugging is a learned skill; don't be discouraged if it takes you a long time to debug your first applet or application.As you gain experience doing it, you'll start to recognize the effects of different classes of bugs and be able to solve each bug in shorter amounts of time.Patience is definitely a virtue in software debugging.Chapter 37Java documentationThe bane of any software developer's existence is embodied by a seemingly innocuous word: documentation.The old coder's war cry, “If it was hard to write, it should be hard to read and use,” has no doubt been heard by some readers
[ Pobierz całość w formacie PDF ]
Linki
- Indeks
- 63. Anderson Kevin J Trylogia Akademii Jedi I W Poszukiwaniu Jedi
- 00.63.735
- 63 3409
- 63 1933
- Chloe Cole [Rock Hard 03] Taming Tai [EC Moderne] (epub)
- Oates Joyce Carol Moja siostra, moja milosc
- § Cornick Nicola Podwójne życie lady Merryn
- Neil Gaiman Księga Cmentarna(1)
- Gutberlet Bernd Ingmar 50 Największych kłamstw i legend w historii Âświata
- Erich von Daniken W Krzyżowym Ogniu Pytań
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- opw.pev.pl