|
|
|
| |
Multithreading with the Java UI in J2ME |
| |
How to ensure lengthy operations don't tie your phone up. |
| |
|
| |
Carl Whalley, 14-Jul-2003 |
|
|
|
|
| Responsiveness is paramount |
|
|
Users require responsiveness from your application above all else. This is particularly important with J2ME as it's
a cardinal sin to lock up a phone. However, there are some operations which by their very nature can't
be interrupted. They are termed "blocking" operations because they block all activity until they are finished.
This presents a problem if you need to use them in your code. A solution is to use Java's multithreading capabilities.
Multithreading allows your program to spin off independent tasks which run until they either complete or are terminated.
Traditionally it has been difficult to use correctly, but it has been made much easier in Java and has been present
from the outset.
|
|
|
An example of this is a J2ME call to connect to a remote host. This will attempt to establish the connection until it either
succeeds or times out, which could take minutes. If your code creates a dialog box with "Connect" as an option, the user
will be happy if there are no problems with the network and a connection is made quickly. If there are problems however, which the user
may know about (such as having no signal), she will quickly become frustrated with this enforced delay which she just wants to cancel.
Putting a "Cancel" button on the dialog won't help, as the event it generates won't be actioned until the blocking operation is complete.
The answer is to display the dialog and spin off the connection operation into its own thread on the "Connect" event. Then the thread
can either complete normally or, as the dialog is still present and responsive, be terminated by a suitable action on the "Cancel"
button.
|
|
| Source example: connecting to remote host |
|
|
Here's some skeleton code to illustrate this. The trick is to put the actual connection code in the same class as the dialog
and pass the dialog reference into the new threads constructor. Then the thread can make calls on the dialog as it progresses, for
example animating it with a progress bar.
|
|
public class ConnectDialog extends Dialog
implements ActionListener {
private Button buttonConnect;
private Button buttonCancel;
private ConnectThread connectThread;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonConnect) {
connect();
}
if (e.getSource() == buttonCancel) {
if (connectThread != null
&& connectThread.isAlive()) {
connectThread.stop();
}
}
return;
}
public void connect() {
connectThread = new ConnectThread(this);
connectThread.start();
}
public void performConnectionOperation() {
// Actual connection is performed here
URL targetUrl = new URL ("http://www.foo.bar");
URLConnection uc=targetUrl.openConnection();
// etc
}
}
public class ConnectThread extends Thread {
private ConnectDialog dialog;
public ConnectThread( ConnectDialog dialog ) {
this.dialog = dialog;
setPriority(1);
}
public void run() {
dialog.performConnectionOperation();
}
}
|
|
| |
 |
|
|
|
|
|
|
|
|
Never before has it been possible to interact with such a vast market. Your business can extend right to
the place you want it the most : your customers. |
|
 |
|
|
Automated ebusiness is now very much an everyday reality across the globe. |
|
|
|
|
|
|
|
|
|
|
|
|
|
Instant access to realtime data. Make updates from any device and see
it change on all connected systems. All views of your data are supplied from one central |
|
 |
|
|
source. Use timely, accurate information where you need it the most - all secured by industry-standard
encryption systems as used by the military and governments. |
|
|
|
|
|
|
|
|
|
|
|
|
|
No-one knows your business better than you. When extending it to the internet your individuality will be
preserved. Methodologies exist which enable tried and
|
|
 |
|
|
tested business systems to be implemented modelling perfectly the business rules and logic you are using today. |
|
|
|
|
|
|
|