banner



How To Register Buttons With Threads Java

The Gallery projection is a unproblematic Java Swing awarding that displays blithe images. You add and remove the images past clicking buttons in the application. Clicking the 'More' button starts a new thread that displays and animates an image. Clicking the 'Less' button stops the about recent thread, stopping the blitheness and removing the image.

Suspending Threads

In this exercise you start debugging the Gallery application and add some images to start some application threads. When yous beginning a debugging session the IDE opens the Debugging window in the left pane of the IDE. The Debugging window displays a list of the threads in the session.

  1. Right-click the Gallery project in the Projects window and choose Debug.

When you click Debug, the IDE starts the Gallery application and opens the default debugging windows. The IDE automatically opens the Debugging window in the left side of the primary window and opens the Debugger Panel in the Output window.

  1. Click 'More' iii times in the Gallery application to first three threads displaying animated images.

If you look in the Debugging window you tin meet that a new thread was started for each animation.

debugging start

Figure 2. Debugging window

  1. Suspend two of the threads by clicking the 'Suspend thread' button to the right of the thread in the Debugging window.

When a thread is suspended, the icon for the thread changes to indicate the new state. You can expand the thread node to view the thread'south call stack. Y'all can correct-click items in the Debugging window to open a popular-up menu with debug commands.

debugging start suspend

Figure iii. Debugging window with two suspended threads

If you wait at the Gallery awarding you can encounter that when you suspended the threads the animation for those threads stopped.

The Debugging window enables you lot to quickly view and change the status of threads in the session. By default the Debugging window displays the Resume and Suspend buttons in the correct side of the window. You can hide the buttons and further customize the display of the Debugging window by using the toolbar at the bottom of the Debugging window. If you are running multiple debugging sessions you can use the driblet downwards list at the height of the Debugging window to choose which session is displayed in the window.

debugging window toolbar

Effigy 4. Debugging window toolbar

Switching Threads

This exercise demonstrates what happens when you are stepping through an application and a different application thread hits a breakpoint. In this do y'all volition set up a method breakpoint and start stepping through the awarding. While yous are stepping through the application you volition start a new thread that volition also hit the breakpoint. The IDE informs you when this occurs past displaying a notification in the Debugging window. Y'all volition and so switch between threads.

  1. In the Gallery awarding window, click 'Less' or 'More' until only ii or iii of the animations are displayed in the window.

  2. In the Projects window of the IDE, expand the gallery bundle and double-click Gallery.java to open the file in the editor.

  3. Insert a method breakpoint in Gallery.java at the beginning of the run method past clicking in the left margin at line 175.

  4. Click 'More' in the Gallery awarding to beginning a new thread that will hitting the method breakpoint.

  5. Click Step Over (F8) and start stepping through the method until the Program Counter reaches line 191.

You tin can meet that the Program Counter in the margin of the editor indicates your position as you lot pace through the method.

  1. Click 'More' in the Gallery application to first a new thread that will hit the method breakpoint.

When the new thread hits the method breakpoint a New Breakpoint Hit notification appears in the Debugging window that informs you that another thread hit a breakpoint while yous were stepping through the method.

debugging newbreakpointhit

Figure five. New Breakpoint Hit notification

When y'all are stepping through a thread and a breakpoint is striking in some other thread, the IDE gives yous the option to switch to the other thread or continue stepping through the current thread. You can click the arrow push in the New Breakpoint Hit notification to switch to the thread that encountered the breakpoint. Yous can switch to the new thread at whatever time by selecting the thread in the notice window. Stepping through the current breakpoint thread resumes the electric current thread but the status of other awarding threads remains unchanged.

Note. If you expect in the Debugging window you can see that the current thread (Thread_Jirka) is indicated by a green bar in the margin. The thread that invoked the notification by striking the breakpoint (Thread_Roman) is indicated past a yellow bar and the thread icon indicates that the thread is suspended past a breakpoint.

debugging current suspended

Figure half-dozen. New Breakpoint Hit notification

  1. Click the pointer in the New Breakpoint Hit notification to switch the electric current thread to the new thread (Thread_Roman).

When y'all switch to the new thread you tin can see the following:

  • The programme counter moves to the position at line 175 in the new current thread (Thread_Roman).

  • A 'suspended thread' notation is now visible in the margin at line 191 indicating that a thread (Thread_Jirka) is suspended at that line.

debugging editor suspendedannot

Effigy 7. Editor showing debugging annotations

  1. Click Pace Over a few times to step through the new electric current thread (Thread_Roman).

  2. Right-click the 'suspended thread' annotation in the editor margin and choose Prepare as Current Thread > Thread_Jirka to switch back to the suspended thread.

debugging editor setcurrent

Effigy eight. Editor showing Set every bit Current Thread pop-up

Alternatively, y'all tin can invoke the Current Thread Chooser (Alt+Shift+T; Ctrl+Shift+T on Mac) and switch to any of the application threads.

debugging thread chooser

Figure 9. Gallery application

When you switch back to Thread_Jirka, the suspended thread annotation appears next to the line where Thread_Roman was suspended. You tin resume Thread_Roman by clicking Resume in the Debugging window.

debugging editor suspendedannot2

Figure 10. Editor showing debugging annotations

The Debugging window enables you to very precisely view and command thread states. The debugger manages application threads to simplify the debugging workflow and to preclude the debugging procedure from creating deadlocks. In this exercise you saw the following beliefs when debugging an application in the IDE.

  • When a thread hits a breakpoint only the breakpoint thread is suspended.

  • When stepping through the application, the current thread is not affected when other application threads hit breakpoints.

  • Stepping only resumes the current thread. When the step is completed only the current thread is suspended.

You tin quit the Gallery application. In the adjacent exercise you will debug the Deadlock application and use the IDE to help you discover a deadlock.

Detecting Deadlocks

The IDE can assistance y'all identify potential deadlock situations by automatically searching for deadlocks amid all suspended threads. When a deadlock is detected, the IDE displays a notification in the Debugging window and identifies the involved threads.

To demonstrate the IDE'southward deadlock detection, you volition run the sample Deadlock project in the debugger and create a deadlock state of affairs.

  1. Expand the myapplication bundle and open Thread1.coffee and Thread2.java in the source editor.

  2. Set a breakpoint in Thread1.java at line 20 and in Thread2.coffee at line 20

To set up the breakpoint, click in the margin of the source editor adjacent to the line where you desire to set the breakpoint. The breakpoint note appears in the left margin next to the line. If you open the Breakpoints window (Alt-Shift-5; Ctrl+Shift+5 on Mac) you tin see that the ii breakpoints are fix and enabled.

debug deadlock setbkpt

Effigy 11. Editor showing breakpoint set at line 20

  1. Correct-click the Deadlock project in the Projects window and choose Debug.

The chief method will run the two threads, and both threads will exist suspended at 1 of the breakpoints. You lot can see the threads suspended by the breakpoints in the Debugging window.

  1. In the Debugging Window, resume the suspended threads ( MyThread1 and MyThread2 ) by clicking the Resume buttons to the right of the suspended threads in the Debugging window.

debug deadlock resume

Figure 12. Resuming suspended threads in the Debugging window

Resuming the threads MyThread1 and MyThread2 will create the deadlock country.

  1. Cull Debug\Cheque for Deadlock from the main menu to check the suspended threads for deadlocks.

debug deadlock detected

Figure 13. Resuming suspended threads in the Debugging window

If y'all check the application for deadlocks and a deadlock is detected, a message appears in the Debugging Window informing you about the deadlock. Y'all tin can see that the threads in deadlock are indicated with a red bar in the left margin of the Debugging window.

This tutorial was a basic introduction to some of the debugging features in the IDE. The Debugging window enables yous to easily suspend and resume threads when debugging an awarding. This tin be extremely helpful when y'all are debugging multi-threaded applications.

Source: https://netbeans.apache.org/kb/docs/java/debug-multithreaded.html

Posted by: watlingtonthestive.blogspot.com

0 Response to "How To Register Buttons With Threads Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel