Skip to main content

Dancing While Gaming – Multitasking VMs on Real Devices

Posted by hiheiss on May 19, 2006 at 8:35 PM PDT

The JavaOne session, “Dancing While Gaming – Multitasking VMs on Real Devices”, focused on helping developers gain a wider and deeper understanding of the Multitasking VM implementation on real devices based on the interaction between Sun and Samsung who are engaged in a joint collaboration through the work of Jae Hyou Lee of Samsung Electronics and Yaniv Shani of Sun.

MVM deployment is based on the Sun JWC1.1.3 (Java Wireless Client 1.1.3) which comes with multi-tasking capabilities. The MVM deploys most of the MSA (Mobile Service Architecture) JSRs. It has been tested with hundreds of games and applications from leading content providers.

So what is MVM? The Multi-Tasking Virtual Machine is a technology that allows the end-user to execute multiple Java applications simultaneously. MVM enables preferred and rich applications like MP3 playing, Instant Messaging and mail clients. Users of MVM want to run multiple Java applications simultaneously and want to be able to switch applications very rapidly. The typical MVM player might be a teenager who loves to listen to MP3 players, chat with friends over the Net, and play video games simultaneously. Multi-tasking capabilities enable all these activities to be conducted on the same device at the same time.

The Isolation Model

To execute multiple applications, the isolation model was introduced in the VM. Each application runs in its own logical Virtual Machine environment, called an isolate. Within the JVM, each isolate is represented as a task. A task consists of one or more Java threads of execution. All tasks execute in the same single OS task context. All tasks share constant data structure and runtime support functions. Each task encapsulates non-sharable program state MVM.

“In the CLCD space, we would like to address limited operating systems that lack native process modeling support,” said Shani. “This is the main reason why in the CLCD space, the VM implementation conducts both the Java task and Java thread scheduling internally within the VM.”

Implementation Details

Each task has a separate, private copy of the static variables of all the loaded Java classes. Static variables are only global to all threads within a particular task. Each task maintains a private copy of ‘count’. The Class representation is shared. Synchronization has to be task private. Threads in one task cannot block threads in another task. Blocking threads across tasks isn’t allowed.

Resource Management

The low-level part of the VM is responsible for all CPU time and memory resources in order to ensure that all tasks get their “fair” share. Other resources, such as network bandwidth and communication ports are managed by the profile layer.

Resource Management – Scheduling

Tasks and threads are scheduled internally by the JVM. Fair scheduling algorithm is used for task scheduling in order to prevent one task from taking disproportionate CPU time and causing other tasks to stop. The ‘isolate’ class offers a set of APIs to modify the priority levels for each task. In most cases MIDlets that execute in the foreground get to have more CPU cycles compared to others that run in the background.

Resource Management – Memory

The VM requires a large consecutive memory block from the underlying OS. Allocations for all tasks are made from this same global heap region. The VM has a bookkeeping mechanism that accounts for each task’s total heap memory consumption. The VM injects OutOfMemoryError as needed to inform tasks that heap space has become scarce.

The Profile Level

The presentation turned to what must be done at the profile level to transform their implementation into one capable of multi-tasking. They have been using Sun’s latest JWC1.1.3, which comes with multi-tasking capabilities. It allows only a single MIDlet to execute in the foreground at a certain time.

A MIDlet application is said to be in the foreground when:

-- Its displayable controls the display.
-- It handles events from the user input mechanism since they are automatically routed to it.

An application is in the background when:

-- Its displayable does not control the display.
-- It does not handle the user input mechanism.
-- Zero or more MIDlets can execute in the Background
at a time.

The application manager is critical for task switching. They use a resident Java MIDlet for the application manager; it is launched in the background when the device ends its boot phase. When users switch it to the foreground, they see a list of all running applications and can switch between them quickly. The app manager also displays background MIDlet alerts, when one wants to drive the user intention.

They next displayed some demos showing how to switch MIDlets to the background and switching between MIDlet states.

Switching a MIDlet to the Background

A short ‘hot’ key press is used for switching between running apps.
Upon a short ‘hot' key press the foreground MIDlet will be
placed in the background and the main device menu will be
displayed on the screen.

Upon a long ‘Hot’ key press the foreground MIDlet will be
placed in the background and the ‘Application manager’ will
be displayed on the screen. The user can see the list of running apps and can switch to another app.

Upon calling setCurrent(null) the MIDlet will be placed in the
background and the ‘idle’ screen of the device will be
displayed. This enables MIDlets to place themselves in the background.

Interrupting the User

A demo of a user experience focused on ways to interrupt the user when a background MIDlet wants to drive the user’s attention.

-- Background MIDlet calls Display.setCurrent(alert)
-- Background MIDlet tries to access a protected API and a security dialog is prompted.
-- Noteworthy event occurred in the background, e.g., Incoming IM while playing a game.

Managing Resources

The session turned to resource management. In a single-tasking environment, the MIDlet has access to all of the available resources. In a multitasking environment, MIDlets have to compete for available resources

Resource management mechanisms include:

Reservation -- A reservation mechanism sets aside a certain amount of a resource for a MIDlet and keeps it available for that MIDlet and that MIDlet alone

Quota -- A quota mechanism permits a MIDlet to allocate up to a
certain amount of a resource. If the MIDlet attempts to allocate more resources than its quota, the attempt fails, even if resources are actually available on the system.

Revocation -- The system can revoke a resource from a MIDlet and give it to another MIDlet. Resource revocation examples include CPU cycles, display access, and audio playback. Resource revocation doesn’t always have a dramatic effect on
the MIDlet’s execution

The session went on to cover LCD display, and sound issues and offered guidelines for resource policy selection. The key points were:

-- Resource policy definition relies on the underlying platform
capabilities and requirements.

-- A fixed partition policy should be used for fundamental resources that are required by the majority of the applications.

-- An open policy should be used for resources that are used by
specific MIDlets and have limited availability (e.g. Bluetooth, Location).

-- The Display and Sound requires special handling.

Multitasking Safety

Regarding safety, the native JVM task can run native code for any task, hence the code must be made aware of the task on whose behalf it is being called and possibly allocating resources. When the task context is established, the code is considered multitasking safe. Sun’s JWC software is multitasking safe.

The session closed with an exploration of such topics as static and global variables, issues of native code execution, MIDlet guidelines, and basic resource awareness principles.

The take-home message from my perspective is that MVM technology fits nicely with what is happening, for better or worse, not only in youth culture in the US and much of the world, but among older generations. We are living speeded up lives and engage in multi-tasking, out of both necessity and pleasure and we want to be able to do it comfortably and quickly from the same device.

“Dancing while gaming” is here to stay and Sun and Samsung are on to something. More power to them.