Java Reference
In-Depth Information
• Swing GUI components are not thread safe. Thread safety is achieved by ensuring that Swing
components are accessed from only the event dispatch thread.
• Performing a lengthy computation in response to a user interface interaction ties up the event
dispatch thread, preventing it from attending to other tasks and causing the GUI components to
become unresponsive. Long-running computations should be handled in separate threads.
• You can extend generic class SwingWorker (package javax.swing ; p. 1011), which implements
Runnable , to perform a task in a worker thread then update Swing components from the event
dispatch thread based on the task's results. You override its doInBackground and done methods.
Method doInBackground performs the computation and returns the result. Method done displays
the results in the GUI.
• Class SwingWorker 's first type parameter indicates the type returned by the doInBackground
method; the second indicates the type that's passed between the publish and process methods
to handle intermediate results.
•Method doInBackground is called from a worker thread. After doInBackground returns, method
done is called from the event dispatch thread to display the results.
•An ExecutionException is thrown if an exception occurs during the computation.
SwingWorker method publish repeatedly sends intermediate results to method process , which
displays the results in a GUI component. Method setProgress updates the progress property.
•Method process executes in the event dispatch thread and receives data from method publish .
The passing of values between publish in the worker thread and process in the event dispatch
thread is asynchronous; process is not necessarily invoked for every call to publish .
PropertyChangeListener (p. 1024) is an interface from package java.beans that defines a single
method, propertyChange . Every time method setProgress is invoked, a PropertyChangeEvent
is generated to indicate that the progress property has changed.
Section 23.12 Timing sort and parallelSort with the Java SE 8 Date/Time API
• Class Instant 's static method now gets the current time.
• To determine the difference between two Instant s, use class Duration 's static method between ,
which returns a Duration object containing the time difference.
Duration method toMillis returns the Duration as a long value milliseconds.
NumberFormat static method getPercentInstance returns a NumberFormat that's used to format
a number as a percentage.
NumberFormat method format returns a String representation of its argument in the specified
numeric format.
Arrays static method parallelSetAll fills an array with values produced by a generator func-
tion that receives an int and returns a value of type int , long or double . Depending on which
overload of method parallelSetAll is used the generator function is an object of a class that im-
plements IntToDoubleFunction (for double arrays), IntUnaryOperator (for int arrays), IntTo-
LongFunction (for long arrays) or IntFunction (for arrays of any non-primitive type).
Arrays static method parallelPrefix applies a BinaryOperator to the current and previous ar-
ray elements and stores the result in the current element.
Section 23.13 Java SE 8: Sequential vs. Parallel Streams
• Streams are easy to parallelize, enabling programs to benefit from enhanced performance on
multi-core systems.
• To obtain a parallel stream, simply invoke method parallel on an existing stream.
Search WWH ::




Custom Search