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