Dancing While Gaming â€“ Multitasking VMs on Real Devices
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.â€
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.
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.
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.
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.