jQuery in Action, 3rd edition
The moose likes Java Micro Edition and the fly likes Multithreading problems in J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Multithreading problems in J2ME" Watch "Multithreading problems in J2ME" New topic

Multithreading problems in J2ME

Salman Faraz

Joined: Feb 14, 2005
Posts: 28
Respected Friends,
I am using J2ME Wireless toolkit to develop a simple application which can establish connection with serial port of the PC, the code which I have written is producing nither any type of exceptions nor any type of compile time errors(while building),but it is giving two strange messages which are as follows:

1) Warning: To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the
commandAction() handler.
The above message is displayed on the command line of wireless tool kit.
2) Serial Test wants to connect to a computer,this may require a data cable.
The second message is displayed in the emulator.

The code which I have written is as follows :

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class SerialTest extends MIDlet implements CommandListener
Display display1;
Command command1;
TextBox textBox1;
Connection connection1;
boolean boolean1;
public void startApp()
display1 = Display.getDisplay(this);
command1 = new Command("OK", Command.OK, 1);
textBox1 = new TextBox("Serial Port", "Click OK for establishing connection", 40, 0);
textBox1 .addCommand(command1);
textBox1 .setCommandListener(this);
display1 .setCurrent(textBox1 );
public void pauseApp()
public void destroyApp(boolean unconditional)
public void commandAction(Command choice, Displayable displayable)
if (choice == command1)
/*Establishing connection with Serial port*/
connection1= Connector.open("comm:com1;baudrate=9600");
/*If a successfull connection is established than true will be printed on the command line*/
catch(Exception e)
System.out.println("unable to establish connection with serial port");

Can any one please tell whats the mistake in the above code and whats the solution to this problem.I would be waiting for your precious replies.

Thanking You,
Salman Faraz.
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169

as far as I have read in other threads the following
1) Warning: To avoid potential deadlock, (...) should be performed in a different thread than the
commandAction() handler.

is only a hint, and needn't be followed if your not interested in implementing Threads, but you have to take the consequences into account.

The next point you mention probably simply comes from you calling:

You probably need to connect to a virtual serial port for the emulator to believe it is connecting to the computer (or a null-modem cable)

- Stuart
Salman Faraz

Joined: Feb 14, 2005
Posts: 28
Respected Mr Stuart Goss,
Thanks a lot for your previous reply,can you also please tell that how to connect to a virtual serial port of the emulator.I would be waiting for your precious reply.

Thanking You,
Salman Faraz.
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
You may actually open the connection inside the commandAction() method, but you must handle the actual communications using another thread, or you�ll definetely risk deadlock.

If you do the actual communication inside commandAction() you won�t be able to handle another command or assynchronous event until the communication ends. Remember that connection calls block the application until done. In the commandAction() case you�ll be blocking the system thread responsible for event delivery.

Below is sample code that you could use to create a thread that opens and handles the connection, plus adjustements in commandAction()

Anyway, this the standard way to do it, you can still put the communication
inside commandAction() anyway but it is dodgy to do it.

1) Runnable implementation

2) commandAction() implementation

Eduardo Marques
Get ready for your SCMAD certification at J2MECertificate.com - See what all the buzz is about ...
Armando Miranda

Joined: May 02, 2005
Posts: 7
Hi Salman,

I have a class that makes it possible. For use it is necesary call first
1. open
2. cPin
3. close

Now from the midlet you have the command listener you make an object an call this methods.

I hope you can get to establish the communication.


Armando M.
Have you checked out Aspose?
subject: Multithreading problems in J2ME
It's not a secret anymore!