You Are Here:

Community: Developer Discussion Boards

#1 Old Question about Threads and processor usage (yield/wait) - 2004-03-03, 00:20

Join Date: Apr 2003
Posts: 61
aspaans
Offline
Regular Contributor
Hello everyone,

I'm writing a MIDlet that is a J2ME emulator of the old Casio FX7000 calculators.
The J2ME emulator executes a Casio program in a separate thread. This allows the user to cancel lengthy operations and looping programs and it keeps the user-interface responsive.

The interpreter that runs the program issues a Thread.yield() every time it moves to the next statement. This ensures that devices with cooperative-threading remain responsive. It all works pretty well, except for this:

Only when a Casio program is busy running: For example (on my Nokia3650) when I press the Menu-button, the menu does not appear (or when holding this button: the task-manager does not appear). Only after the program ends, suddenly the phone's menu appears. But all events (keypresses/paints) that are handled by the MIDlet itself arrive in time and are handled promptly.

When a Casio program is not running: Everything works fine (the interpreter-thread is waiting at the wait() statement).

Is there a way around this? What do i need to call? Thread.yield() does not work. Nor wait(timeout) or sleep(timeout)... I'd like to solve this, especially for the case when a Casio program is running and the user receives a phone-call.

Code:
// Simplified pseudo-code sample of the Runnable's run() method.

while (mActive)
{
    synchronized(this)
    {    
        wait(); // waiting until EXE button is pressed
        // The user pressed the EXE button. Run the program.
    }
    Interpreter.run(program);
    // done.
    ...
}

Interpreter.run(Program program)
{
    int i = 0;
    // Casio program about to be busy running.
    while (i < program.mStatements.size())
    {
        Thread.yield();

        ... execute(program.mStatements.get(i));
        ...

        i++;
    }
}
Instead of Thread.yield(), should I call something else?
Thanks!
Reply With Quote

#2 Old 2004-03-03, 17:20

Join Date: Apr 2003
Posts: 61
aspaans
Offline
Regular Contributor
Update: When running on the emulator, these issues do not occur. Only when running on the real device.
I made some changes that allows a phone-call to come in or some other event to be handled promptly.

Code:
// Simplified pseudo-code sample of the Runnable's run() method.

while (mActive)
{
    synchronized(this)
    {    
        wait(); // waiting until EXE button is pressed
        // The user pressed the EXE button. Run the program.
    }
    Interpreter.run(program);
    // done.
    ...
}

Interpreter.run(Program program)
{
    int i = 0;
    // Casio program about to be busy running.
    while (i < program.mStatements.size())
    {
        Thread.yield();

        synchronized(mSyncer)
        {
            if (mHalted)
                mSyncer.wait();
        }

        ... execute(program.mStatements.get(i));
        ...

        i++;
    }
}

Interpreter.halt()
{
    synchronized(mSyncer)
    {
        mHalted = true;
    }
}

Interpreter.resume()
{
    synchronized(mSyncer)
    {
        mHalted = false;
        mSyncer.notify();
    }
}
Where the halt() and resume() are called when the canvas loses or gains focus (called from the hideNofity/showNotify and when switching displayables).

This still does not allow the Menu keypress to be handled promptly, but it will halt execution until the app gets focus again.

BTW: Is there a way to catch the Menu keypress on Series60 phones?
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia