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:

myths said...

hey thank u
it is very useful

Anonymous said...

thanks a lot!!!

Anonymous said...

Type article! Wonderful!

Anonymous said...

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

Anonymous said...

Thank you! this has been very helpful!

:)

Anonymous said...

you are awesome!! thanks man!

Grissa MED ALI said...

thx a lot !! it workds

current

last archive