Home | History | Annotate | Download | only in multiple-threads
      1 page.title=Specifying the Code to Run on a Thread
      2 
      3 trainingnavtop=true
      4 @jd:body
      5 
      6 <div id="tb-wrapper">
      7 <div id="tb">
      8 
      9 <!-- table of contents -->
     10 <h2>This lesson teaches you to</h2>
     11 <ol>
     12   <li><a href="#ExtendClass">Define a Class that Implements Runnable</a></li>
     13   <li><a href="#RunMethod">Implement the run() Method</a>
     14 </ol>
     15 
     16 <h2>You should also read</h2>
     17 <ul>
     18   <li><a href="{@docRoot}guide/components/processes-and-threads.html">Processes and Threads</a></li>
     19 </ul>
     20 
     21 
     22 <h2>Try it out</h2>
     23 <div class="download-box">
     24     <a href="{@docRoot}shareables/training/ThreadSample.zip" class="button">Download the sample</a>
     25     <p class="filename">ThreadSample.zip</p>
     26 </div> <!-- download-box -->
     27 
     28 </div> <!-- tb -->
     29 </div> <!-- tb-wrapper -->
     30 
     31 <p>
     32     This lesson shows you how to implement a {@link java.lang.Runnable} class, which runs the code
     33     in its {@link java.lang.Runnable#run Runnable.run()} method on a separate thread. You can also
     34     pass a {@link java.lang.Runnable} to another object that can then attach it to a thread and
     35     run it. One or more {@link java.lang.Runnable} objects that perform a particular operation are
     36     sometimes called a <i>task</i>.
     37 </p>
     38 <p>
     39     {@link java.lang.Thread} and {@link java.lang.Runnable} are basic classes that, on their own,
     40     have only limited power. Instead, they're the basis of powerful Android classes such as
     41     {@link android.os.HandlerThread}, {@link android.os.AsyncTask}, and
     42     {@link android.app.IntentService}. {@link java.lang.Thread} and {@link java.lang.Runnable} are
     43     also the basis of the class {@link java.util.concurrent.ThreadPoolExecutor}. This class
     44     automatically manages threads and task queues, and can even run multiple threads in parallel.
     45 </p>
     46 <h2 id="ExtendClass">Define a Class that Implements Runnable</h2>
     47 <p>
     48     Implementing a class that implements {@link java.lang.Runnable} is straightforward. For example:
     49 </p>
     50 <pre>
     51 public class PhotoDecodeRunnable implements Runnable {
     52     ...
     53     &#64;Override
     54     public void run() {
     55         /*
     56          * Code you want to run on the thread goes here
     57          */
     58         ...
     59     }
     60     ...
     61 }
     62 </pre>
     63 <h2 id="RunMethod">Implement the run() Method</h2>
     64 <p>
     65     In the class, the {@link java.lang.Runnable#run Runnable.run()} method contains the
     66     code that's executed. Usually, anything is allowable in a {@link java.lang.Runnable}. Remember,
     67     though, that the {@link java.lang.Runnable} won't be running on the UI thread, so it can't
     68     directly modify UI objects such as {@link android.view.View} objects. To communicate with
     69     the UI thread, you have to use the techniques described in the lesson
     70     <a href="communicate-ui.html">Communicate with the UI Thread</a>.
     71 </p>
     72 <p>
     73     At the beginning of the {@link java.lang.Runnable#run run()} method, set the thread to use
     74     background priority by calling
     75     {@link android.os.Process#setThreadPriority Process.setThreadPriority()} with
     76     {@link android.os.Process#THREAD_PRIORITY_BACKGROUND}. This approach reduces
     77     resource competition between the {@link java.lang.Runnable} object's thread and the UI
     78     thread.
     79 </p>
     80 <p>
     81     You should also store a reference to the {@link java.lang.Runnable} object's
     82     {@link java.lang.Thread} in the {@link java.lang.Runnable} itself, by calling
     83     {@link java.lang.Thread#currentThread() Thread.currentThread()}.
     84 </p>
     85 <p>
     86     The following snippet shows how to set up the {@link java.lang.Runnable#run run()} method:
     87 </p>
     88 <pre>
     89 class PhotoDecodeRunnable implements Runnable {
     90 ...
     91     /*
     92      * Defines the code to run for this task.
     93      */
     94     &#64;Override
     95     public void run() {
     96         // Moves the current Thread into the background
     97         android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
     98         ...
     99         /*
    100          * Stores the current Thread in the PhotoTask instance,
    101          * so that the instance
    102          * can interrupt the Thread.
    103          */
    104         mPhotoTask.setImageDecodeThread(Thread.currentThread());
    105         ...
    106     }
    107 ...
    108 }
    109 </pre>
    110