Sun Microsystems 2 Wireless Office Headset User Manual


 
8 Multitasking Guide May 2007
Maintain the following invariants:
A value of zero means a NULL pointer
A nonzero value means a valid native pointer
In native code, when you allocate memory, use KNI field access to store the pointer
in the private field. When you free the native memory, use KNI field access to store
0 in the field. Using the K Native Interface (KNI) field access avoids race conditions.
Have operations that use the native pointer use KNI field access for consistency.
Have those operations check for a NULL value before they use the pointer, and throw
an appropriate exception if the field does not have a pointer. An appropriate
exception is a NullPointerException.
Singletons
CLDC HotSpot Implementation 2.0 isolates the logical virtual machines, but there is
one situation to consider if you add or change Java code: Singletons. Singleton
classes are often used when only one instance of a class should exist. Sometimes the
class itself is used as a singleton, and no instance of it is ever created. The way to
handle singleton depends on whether you mean it to be used on a per-application
(that is, per-task) basis or whether you mean it to be used by the entire system.
For example, in Java Wireless Client software, an event queue is a per-task singleton,
because each task has its own event queue. Per-task singletons are not a problem as
they are handled by the VM. Static state is automatically replicated on a per-task
basis. On the other hand, the singleton that holds the foreground display is a
system-global singleton. Only one Display can be in the foreground in the entire
system. All other display instances must be in the background. System-global
singletons require additional work.
To handle a system-global singleton, consider either maintaining the singleton’s state
in a single task (such as the AMS task) and communicate the updates through
messages, or migrating key pieces of information into native memory, with access to
them arbitrated through native methods.
If you maintain the singleton’s state in a single task and update it using events, be
aware that events are asynchronous messages. Organize the updates so that
operations are tolerant of being executed out of order. Although messages are
generally processed in the order received, messages from different tasks might not
be processed in the order they are sent. Also organize the updates so that requestors
can proceed asynchronously. Asynchronous messages do not have any
acknowledgement by default, so the sender cannot know when the message is
processed or whether it is processed successfully.