Sunday, February 24, 2008

J2ME Mobile Network Usage - Avoid deadlock

Avoid deadlock situations while connecting to network resources from mobiles. I used to get the following warning while trying to use network resources during a j2me application development:

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.

Here is the way to counter this problem by using a separate thread in order to connect to the network. This ensures avoidance of deadlock situations in your application. In the run method I am calling the getConn() method which connects to the network resource. The next switchDisplayable method was a business requirement to goto the next screen.

new Thread(new Runnable() {

public void run() {
try {
getConn();
switchDisplayable(null, getWaitScreenRegRoom());
} catch (Exception e) {
}
}
}).start();


Comment with better approaches.

1 comment:

Unknown said...

Hey,I tried with differernt thread but it runs only some advanced phone Eg E90.It hangs in ordinary phone sets. could you suggest how to solve with out threads.
Thanks,
jogi
pauljogi@gmail.com