Multiple threads are available within a Java virtual machine. Both the MIDP implementor and the app developer need to use these threads safely. Using threads safely means that shared data never becomes corrupted, even in the presence of concurrent access, and that the system is live (threads do not deadlock).

Thread Safety

The Java Tutorial: A Short Course on the Basics (Third version) [16], has information on how to properly protect shared information in the Java coding language. This section provides some MIDP-specific suggestions and requirements. apps must be allowed to define and implement their own locking policies for their own data structures, without any restrictions from LCDUI beyond those imposed by the MIDP 2.0 Specification [19]. apps should be free to use locks even on LCDUI objects, and to create subclasses of LCDUI classes without violating any safety guarantees.

MIDP Implementors

Strongly Recommend: Java graphics bulb1_icon.gif Ensure that calls into LCDUI from any thread or class outside the package leave LCDUI in a valid and consistent state. For example, it should not be possible for an app's threads to concurrently update a list, and to have some changes appear but not others. If one thread adds two elements to a list, while another changes two existing elements, the list should, after running the calls, have the two updated elements and the two new elements.

app Developers

If your MIDlet is multithreaded, define and use a locking policy in your app that protects its shared data without deadlocking.

Strongly Recommend: Java graphics bulb1_icon.gif Build into your locking policy the MIDP 2.0 Specification requirement regarding the serviceRepaints method of the Canvas class. That requirement states that if your app calls the serviceRepaints method, it does so without holding any lock that your implementation of the paint method might acquire. If you violate this prohibition, deadlock could result.


In addition to keeping their apps deadlock free and safe, app developers must also be concerned about responsiveness. (See "Make It Responsive" on page 10 for information.) Specifically, callbacks need to be responsive, and they can be most responsive when multiple threads are used appropriately. A callback is a method that an app developer writes and a device calls to perform a task at appropriate times. A MIDP implementation informs a MIDlet about user actions, such as selecting an abstract command, through callbacks, such as the commandAction method.

Strongly Recommend: Java graphics bulb1_icon.gif Do not perform any task in a callback that has the potential to take a long time, such as network operations. Instead, arrange for the work to be done in another thread. (See Networking, User Experience, and Threads [27] and Making HTTP Connections Using Background Threads [28] for information.)