Sunday, November 18, 2007

J2ME : Create New Thread under commandAction

Guys, if you happen receive this warning, "To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the commandAction() handler." and your J2ME application doesn't work properly, here is quick work around to do it right :

here is the original lines :


public void commandAction(Command c, Displayable s)
{
if (c == cmCall)
{
display.setCurrent(this);
try
{
call_some_function_that_call_network_function();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (c == cmExit)
{
// the rest command
}
}


and here is the modified lines :

public void commandAction(Command c, Displayable s)
{
if (c == cmCall)
{
display.setCurrent(this);
try
{
new Thread(new Runnable()
{
public void run()
{
try{
call_some_function_that_call_network_function();
} catch(IOException e) {}
}
}).start();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
else if (c == cmExit)
{
// the rest command
}
}

7 comments:

  1. Type article! Wonderful!

    ReplyDelete
  2. Hi!
    What’s the difference between adult dating sites and normal dating sites?
    please no offtopic $)

    ReplyDelete
  3. Thank you! this has been very helpful!

    :)

    ReplyDelete
  4. you are awesome!! thanks man!

    ReplyDelete