×
Namespaces

Variants
Actions
(Difference between revisions)

Creating threads in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
hamishwillee (Talk | contribs)
m (Hamishwillee - Tidy wiki text)
Line 1: Line 1:
 
[[Category:Java ME]][[Category:Code Examples]][[Category:Threading]][[Category:Symbian]][[Category:Series 40]][[Category:Java Runtime 2.3 for Symbian]][[Category:Nokia Belle]][[Category:S60 3rd Edition (initial release)]][[Category:S60 5th Edition]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
 
[[Category:Java ME]][[Category:Code Examples]][[Category:Threading]][[Category:Symbian]][[Category:Series 40]][[Category:Java Runtime 2.3 for Symbian]][[Category:Nokia Belle]][[Category:S60 3rd Edition (initial release)]][[Category:S60 5th Edition]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
 +
{{Abstract|This code snippet demonstrates how to create and execute a separate thread.}}
 +
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= [[Media:CreatingThreadsSource.zip]]
 
|sourcecode= [[Media:CreatingThreadsSource.zip]]
Line 15: Line 17:
 
|translated-from-title= <!-- Title only -->
 
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by= <!-- After re-review: [[User:username]] -->
+
|review-by= [[User:mtilli]]
|review-timestamp= <!-- After re-review: YYYYMMDD -->
+
|review-timestamp= 20120813
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
Line 22: Line 24:
 
|author= [[User:Dekudin]]
 
|author= [[User:Dekudin]]
 
<!-- The following are not in current metadata -->
 
<!-- The following are not in current metadata -->
|subcategory= Base/System
 
 
|id= CS001321
 
|id= CS001321
 
}}
 
}}
  
 
 
==Overview==  
 
==Overview==  
  
This code snippet demonstrates how to create and execute a separate thread.
+
There are two ways to implement new threads in Java ME:  
 
+
by extending the base {{Icode|Thread}} class, or  
There are two ways to implement new threads:  
+
by implementing the {{Icode|Runnable}} interface.  
- by extending the base {{Icode|Thread}} class, or  
+
- by implementing the {{Icode|Runnable}} interface.  
+
  
 
In both cases, a new class based on the {{Icode|Thread}} class or the {{Icode|Runnable}} interface must be defined and it must override the {{Icode|run()}} method. This method defines the code that the thread will run, and thus defines the thread's functionality.  
 
In both cases, a new class based on the {{Icode|Thread}} class or the {{Icode|Runnable}} interface must be defined and it must override the {{Icode|run()}} method. This method defines the code that the thread will run, and thus defines the thread's functionality.  
Line 39: Line 37:
 
To use the previously defined {{Icode|Thread}} class, an object should be instanced and its {{Icode|start()}} method should be invoked.
 
To use the previously defined {{Icode|Thread}} class, an object should be instanced and its {{Icode|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 {{Icode|Thread}} class constructor. Also in this case, the {{Icode|start()}} method of the newly created {{Icode|Thread}} class should be invoked.
+
If the {{Icode|Runnable}} interface is used instead, an object should be instanciated, after which the object should be passed to the {{Icode|Thread}} class constructor. Also in this case, the {{Icode|start()}} method of the newly created {{Icode|Thread}} class should be invoked.
  
 
The following code snippet uses the first way of implementing a thread - it extends the base {{Icode|Thread}} class and overrides the {{Icode|run()}} method.
 
The following code snippet uses the first way of implementing a thread - it extends the base {{Icode|Thread}} class and overrides the {{Icode|run()}} method.
Line 46: Line 44:
  
 
This MIDlet consists of 2 source files:
 
This MIDlet consists of 2 source files:
 
 
# CreatingThreadsMidlet.java - contains the MIDlet class.
 
# CreatingThreadsMidlet.java - contains the MIDlet class.
 
# GaugeThread.java - contains the GaugeThread class which updates the specified gauge in a separate thread.
 
# GaugeThread.java - contains the GaugeThread class which updates the specified gauge in a separate thread.
 +
  
 
==Source file: CreatingThreadsMidlet.java==
 
==Source file: CreatingThreadsMidlet.java==

Revision as of 09:17, 28 September 2012

This code snippet demonstrates how to create and execute a separate thread.

Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131, Nokia 701, Nokia Asha 305
CompatibilityArticle
Keywords: java.lang.Thread, java.lang.Runnable, java.lang.Runnable.run, java.lang.Thread.start, java.lang.Thread.join, java.lang.Thread.isAlive
Created: dekudin (25 Feb 2009)
Reviewed: mtilli (13 Aug 2012)
Last edited: hamishwillee (28 Sep 2012)

Contents

Overview

There are two ways to implement new threads in Java ME:

  • 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 instanciated, 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:

  1. CreatingThreadsMidlet.java - contains the MIDlet class.
  2. GaugeThread.java - contains the GaugeThread class which updates the specified gauge in a separate thread.


Source file: CreatingThreadsMidlet.java

    // 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.
}
}
}


Source file: GaugeThread.java

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;
}
}
}
}

Postconditions

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.

160 page views in the last 30 days.
×