MultiThread

Put your problem here if it does not fit any of the other categories.

MultiThread

Postby khoinguyen » Fri May 02, 2008 2:56 am

Hi,
Please help me how to use multi thread in Android.
Thanks all.
khoinguyen
Freshman
Freshman
 
Posts: 5
Joined: Sat Mar 22, 2008 4:16 pm

Top

Threads

Postby daniel.kick.ass » Fri May 02, 2008 7:48 pm

Hi,

You create threads the same way you create them with any other JDK:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyThread extends Thread {
  2.  
  3. public void run() {
  4.  
  5. // Your implementation
  6.  
  7. }
  8.  
  9. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


To start this thread from your code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Thrad t = new MyThread();
  2.  
  3. t.start();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


You can also implement a runnable interface:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyClass extends Activity implements Runnable {
  2.  
  3. public void run() {
  4.  
  5. // Your implementation
  6.  
  7. }
  8.  
  9. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


and to start this thread from the same class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Thread mThread;
  2.  
  3. ...
  4.  
  5. if (mThread == null) {
  6.  
  7. mThread = new Thread(this);
  8.  
  9. mThread.start();
  10.  
  11. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


NOTE: Using the 2nd technique will NOT allow you to close the Activity thread before the new thread terminates.

Daniel
daniel.kick.ass
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 22, 2007 9:54 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests