Introduction to threading in J2ME

I know from experience the idea of threads can be a little intimidating at first. So I will begin by addressing what is a thread and why you need to use it.

A thread is a way to hand out some time-intensive tasks to a separate process within your application, without keeping the “main” thread/process busy, this main thread is the primary entry point to your application, exactly like the function main() is in many programming languages such as C and Java. Think of it to be like hiring a contractor to finish your basement. Even though you are handy enough to accomplish that task, you simply have no time for such a task!

What kind of tasks are we talking here?  Well, some tasks such as TCP/IP communication (e.g. working with some API over HTTP), some big loop (e.g. building an index of the user’s MP3 collection) or even displaying an animated GIF, those tasks would take too much time on a device with limited process power, this is time on the scale of CPU cycles or microseconds, not our scale of seconds or minutes.

So, why do I have to use threads for those tasks? Well, I think this is better explained if we explored what happens if you don’t use threads and try to run some time-intensive task in the main thread:

  1. Your application will freeze and appear unresponsive until the task is accomplished, and you won’t be able to give your users any means to cancel this task while it is executing (again, because your application is locked up).
  2. On devices like the BlackBerry, your application will be terminated. Some (if not most) devices do not allow the “main” thread to consume too much CPU for too long (a.k.a become blocking), otherwise they assume your application compromises the device’s stability and shut it down.

It is always a good idea to keep the main thread for GUI and hand off grunt work to threads. One thing you should keep in mind, those threads do not communicate with the main thread unless you write code to do so.

In J2ME, creating a thread is pretty simple. The easiest and quickest way is to extend the “Thread” class and overload the run() function on-the-fly, then “start” the thread as follows:

Thread myThread = new Thread() {

public void run() {

// do whatever you need to do here

}

};

myThread.start();

Remember to properly catch and handle exceptions in your thread, otherwise they could bubble up and cause your application to terminate.

You’ll get an exception if you try to manipulate the GUI within your thread, because GUI is assumed to be a responsibility of the main thread only. This how you can do it, which can also be used to report progress to the main thread for something like a progress bar:

UiApplication.getUiApplication().invokeLater(new Runnable() {

public void run() {

// do whatever you need here!

}

};

getUiApplication() gets us the class containing the main() function, i.e. the entry point of your application and the main thread. invokeLater() is a way of “scheduling” your code within Runnable.run(). I put scheduling in double quotes because technically your code has to wait until all pending events on the main thread are processed, but practically that takes no time. However, if this becomes a problem you could use invokeAndWait() instead of invokeLater() to block your thread until the main thread processes your request.

That’s all, enjoy and remember to drop us a line if you find this to be helpful!