Question

I am developing a Java-ME Based Mobile Application. Now My Requirements are like whenever I am updating one of my RMS, I want my application to be stay in a Freeze kind of mode; which means no other action like clicking button or anything else should happen. My Method is already "Synchronized".

Kindly guide me regarding this question.

Thanks.

Was it helpful?

Solution

The best way to handle this is to "serialize" your tasks. You can do this with a message queue - a class that maintains a Vector of message objects (tasks) and runs code based on each message. The queue runs on a thread that processes each task (message) in series. You create a simple message class for the different tasks - read RMS etc. A message can be an Integer if you like that wraps a number. The operation of adding and retrieving messages is synchronized but the code than does the tasks is not and runs on a simple switch block. The benefit of serializing your tasks is you don't have to worry about concurrency. Here is some of the essential code from a class I use to do this.

class MessageQueue implements Runnable{
Vector messages;
Thread myThread;
volatile boolean stop;

public void start() {
stop=false;
myThread=new Thread(this);
myThread.start();
}

// add message to queue - this is public
public synchronized void addMessage(Message m) {
messages.addElement(m);
if(stop) {
    start();
} else {
    // wake the thread
notify();
    }
}

// get next message from queue - used by this thread
private synchronized Message nextMessage() {
if(stop) return null;
if(messages.isEmpty()) {
    return null;
    } else {
    Message m=(Message)messages.firstElement();
    messages.removeElementAt(0);
    return m;
   }
}

public void run() {
    while (!stop) {
        // make thread wait for messages
        if (messages.size() == 0) {
            synchronized (this) {
                try {
                    wait();
                } catch (Exception e) {

                }
            }
        }
        if (stop) {
            // catch a call to quit
            return;
        }
        processMessage();
    }
}
}

// all the tasks are in here
private void processMessage() {
    Message m = nextMessage();
    switch (m.getType()) {
        case Message.TASK1:
            // do stuff
            break;
        case Message.TASK2:
            // do other stuff
            break;
        case Message.TASK3:
            // do other other stuff
            break;
        default: //handle bad message
        }
    }
}

OTHER TIPS

What you are asking is very code depended. Usually when you want to make some synchronic actions you just write them one after the other. in java it's more complected, since sometimes you "ask" the system to do something (like repaint() method). But since the RMS read/write operations are very quick (few millisecond) i don't see any need in freesing.

Could you please provide some more information about the need (time for RMS to respond)? does your code runs on system thread (main thread) or your own thread?

I want my application to be stay in a Freeze kind of mode; which means no other action like clicking button or anything else should happen.

First of all I would strongly advise against real freezing of UI - this could make a suicidal user experience for your application.

If you ever happened to sit in front of computer frozen because of some programming bug, you may understand why approach like this is strongly discouraged. As they describe it in MIDP threading tutorial, "user interface freezes, the device appears to be dead, and the user becomes frustrated..."

This tutorial by the way also suggests possibly the simplest solution for problems like you describe: displaying a wait screen. If you don't really have reasons to avoid this solution, just do as tutorial suggests.

To be on a safe side, consider serializing tasks as suggested in another answer. This will ensure that when RMS update starts, there are no other tasks pending.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top