| ID | CS001321 | Creation date | March 30, 2009 |
| Platform | S60 3rd Edition, S60 5th Edition, S40 2nd Edition, S40 3rd Edition, FP1 | Tested on devices | Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131 |
| Category | Java ME | Subcategory | Base/System |
| Keywords (APIs, classes, methods, functions): java.lang.Thread, java.lang.Runnable, java.lang.Runnable.run, java.lang.Thread.start, java.lang.Thread.join, java.lang.Thread.isAlive |
This code snippet demonstrates how to create and execute a separate thread.
There are two ways to implement new threads: - by extending the base Thread class, or - by implementing the Runnable interface.
In both cases, a new class based on the Thread class or the Runnable interface must be defined and it must override the run() method. This method defines the code that the thread will run, and thus defines the thread's functionality.
To use the previously defined Thread class, an object should be instanced and its start() method should be invoked.
If the Runnable interface is used instead, an object should be instanced, after which the object should be passed to the Thread class constructor. Also in this case, the start() method of the newly created Thread class should be invoked.
The following code snippet uses the first way of implementing a thread - it extends the base Thread class and overrides the run() method.
Note that the state of the UI control in this snippet is changed from separate thread. UI controls in Java are thread-safe, so it is safe to update states and execute methods of UI controls from separate threads.
This MIDlet consists of 2 source files:
// Gauge to be updated by our gauge thread.
private Gauge gauge;
// Gauge thread
private GaugeThread gaugeThread;
/**
* Sets up the main form.
*/
private void setupMainForm() {
mainForm = new Form("Creating Threads");
// Create gauge for our thread
gauge = new Gauge("Thread activity", false, Gauge.INDEFINITE,
Gauge.INCREMENTAL_IDLE);
mainForm.append(gauge);
logField = new TextField("Log", null, 512, TextField.PLAIN);
mainForm.append(logField);
mainForm.addCommand(EXECUTE_COMMAND);
mainForm.addCommand(EXIT_COMMAND);
mainForm.setCommandListener(this);
}
/**
* Executes the snippet.
*/
private void executeSnippet() {
startGaugeThread();
}
/**
* Creates gauge thread and starts it
*/
private void startGaugeThread() {
stopGaugeThread();
// Create thread for gauge and run it
gaugeThread = new GaugeThread(gauge);
gaugeThread.start();
}
/**
* Stops gauge thread
*/
private void stopGaugeThread() {
// Stop gauge thread
if(gaugeThread != null && gaugeThread.isAlive() == true) {
gaugeThread.quit();
try {
// Wait for gauge thread to die
gaugeThread.join();
} catch(InterruptedException exc) {
// This thread was interrupted, do nothing.
}
}
}
import javax.microedition.lcdui.Gauge;
/**
* Gauge thread functionality. Updates specified gauge.
*/
public class GaugeThread extends Thread {
// Gauge to be updated.
private Gauge gauge = null;
// Flag indicating that thread must quit.
private boolean isQuit = false;
/**
* Constructor.
* @param gauge - gauge to be updated by this thread
*/
public GaugeThread(Gauge gauge) {
this.gauge = gauge;
}
/**
* Terminates this thread.
*/
public void quit() {
isQuit = true;
}
/**
* Implements functionality of thread.
*/
public void run() {
// If no gauge specified exit from thread
if(gauge == null) {
return;
}
// Thread loop
while(isQuit == false) {
// Update gauge value
gauge.setValue(Gauge.INCREMENTAL_UPDATING);
try {
// Make thread sleep for 100 milliseconds
sleep(100);
} catch(InterruptedException interruptExc) {
// Some other thread interrupts this thread so exit from here
return;
}
}
}
}
The gauge and the logging text box are shown on the display. Press 'Execute snippet' to start a thread which updates the gauge's state so that its animation is displayed.
This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.
No related wiki articles found