JThreadKit Home

SyncInteger Demo - Instructions

Overview

SyncInteger is used to signal int values among two or more threads. SyncInteger encapsulates Java's wait-notify mechanism making it simple and safe for one thread to wait for a certain value to be set by another thread. In fact, any number of threads can be waiting and any number of threads can safely change the value. ***More info???***

Running the Demo

When the demo is launched, a SyncInteger is created behind the scenes. It's initial value is set to zero, and you can see it displayed in the right pane.

Changing The Value - The value of the SyncInteger can be changed (+/- 1) by either clicking the increase (>) or decrease (<) buttons located to the sides of the current value display. In addition, you can specify a specific value in the "New Value" text field and click "change".

Changing The Range - The minimum/maximum range of values is also displayed. By default they are Integer.MAX_VALUE and Integer.MIN_VALUE. These values can be changed by clicking the "change" button and entering a new range.

Calling a Method - The left pane contains a list of methods that can be called on the SyncInteger Object. Choose a method from the list and click "Start", or simply double-click the list item. A dialog will be displayed asking for additional information based on the method. Once the appropriate information has been entered, the demo will begin. All results are displayed in the text area in the right pane.

Example - waitForValueToChange() is double-clicked. A dialog appears requiring a timeout value to be entered. Zero is entered, equivalent to no timeout, and the demo begins. The left pane is now disabled since we are waiting, in this case potentially forever, for the value of the SyncInteger to change. The "<" button is now clicked, and information related to how long it took for the value to change is displayed in the text area.

The following features are highlighted by the demo:

  • Swing Helper


  • Each time a method is called, a helper thread needs to be created. Instead of creating a brand new thread each time, the SwingHelper is used (add'l info...). The static method SwingHelper.execute() takes two runnable objects as arguments. The first being a background task to be done by a helper thread. The second a bundle of work that will be passed off for the event thread to run. The demo uses the helper thread to call the appropriate method. Once it is complete, the other runnable is used to safely re-activate the screen. Behind the scenes, the SwingHelper has a Thread Pool which recycles the threads so we don't need to keep recreating new Threads, saving resources by limiting the number of Objects created.

  • JTKLabel


  • The SyncInteger's current value is actually a JTKLabel. A JTKLabel is a JThreadkit component that adds additional functionality to a standard JLabel. The feature specifically highlighted in the demo is the ability to set the label text safely without calling SwingUtilities.invokeLater(). You simply call the setTextSafely() on the JTKLabel object, and the JTKLabel ensures that the event thread updates the label text. JTKLabels are also used to display the minimum/maximum range values.

  • Wait-Notify


  • This is displayed throughout the demo. With all the methods called in the demo, we create a helper thread (using SwingHelper) to wait for a specific condition. Once that condition has been met (assuming the timeout period has not passed), the helper thread is notified and stops waiting.