summaryrefslogtreecommitdiffstats
path: root/docs/html/training/multiple-threads/define-runnable.jd
blob: 40853d3329cd72146ec83d10820ff0a6235c1cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
page.title=Specifying the Code to Run on a Thread

trainingnavtop=true
@jd:body

<div id="tb-wrapper">
<div id="tb">

<!-- table of contents -->
<h2>This lesson teaches you to</h2>
<ol>
  <li><a href="#ExtendClass">Define a Class that Implements Runnable</a></li>
  <li><a href="#RunMethod">Implement the run() Method</a>
</ol>

<h2>You should also read</h2>
<ul>
  <li><a href="{@docRoot}guide/components/processes-and-threads.html">Processes and Threads</a></li>
</ul>


<h2>Try it out</h2>
<div class="download-box">
    <a href="{@docRoot}shareables/training/ThreadSample.zip" class="button">Download the sample</a>
    <p class="filename">ThreadSample.zip</p>
</div>
</div>

</div>
</div>

<p>
    This lesson shows you how to implement a {@link java.lang.Runnable} class, which runs the code
    in its {@link java.lang.Runnable#run Runnable.run()} method on a separate thread. You can also
    pass a {@link java.lang.Runnable} to another object that can then attach it to a thread and
    run it. One or more {@link java.lang.Runnable} objects that perform a particular operation are
    sometimes called a <i>task</i>.
</p>
<p>
    {@link java.lang.Thread} and {@link java.lang.Runnable} are basic classes that, on their own,
    have only limited power. Instead, they're the basis of powerful Android classes such as
    {@link android.os.HandlerThread}, {@link android.os.AsyncTask}, and
    {@link android.app.IntentService}. {@link java.lang.Thread} and {@link java.lang.Runnable} are
    also the basis of the class {@link java.util.concurrent.ThreadPoolExecutor}. This class
    automatically manages threads and task queues, and can even run multiple threads in parallel.
</p>
<h2 id="ExtendClass">Define a Class that Implements Runnable</h2>
<p>
    Implementing a class that implements {@link java.lang.Runnable} is straightforward. For example:
</p>
<pre>
public class PhotoDecodeRunnable implements Runnable {
    ...
    &#64;Override
    public void run() {
        /*
         * Code you want to run on the thread goes here
         */
        ...
    }
    ...
}
</pre>
<h2 id="RunMethod">Implement the run() Method</h2>
<p>
    In the class, the {@link java.lang.Runnable#run Runnable.run()} method contains the
    code that's executed. Usually, anything is allowable in a {@link java.lang.Runnable}. Remember,
    though, that the {@link java.lang.Runnable} won't be running on the UI thread, so it can't
    directly modify UI objects such as {@link android.view.View} objects. To communicate with
    the UI thread, you have to use the techniques described in the lesson
    <a href="communicate-ui.html">Communicate with the UI Thread</a>.
</p>
<p>
    At the beginning of the {@link java.lang.Runnable#run run()} method, set the thread to use
    background priority by calling
    {@link android.os.Process#setThreadPriority Process.setThreadPriority()} with
    {@link android.os.Process#THREAD_PRIORITY_BACKGROUND}. This approach reduces
    resource competition between the {@link java.lang.Runnable} object's thread and the UI
    thread.
</p>
<p>
    You should also store a reference to the {@link java.lang.Runnable} object's
    {@link java.lang.Thread} in the {@link java.lang.Runnable} itself, by calling
    {@link java.lang.Thread#currentThread() Thread.currentThread()}.
</p>
<p>
    The following snippet shows how to set up the {@link java.lang.Runnable#run run()} method:
</p>
<pre>
class PhotoDecodeRunnable implements Runnable {
...
    /*
     * Defines the code to run for this task.
     */
    &#64;Override
    public void run() {
        // Moves the current Thread into the background
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        ...
        /*
         * Stores the current Thread in the PhotoTask instance,
         * so that the instance
         * can interrupt the Thread.
         */
        mPhotoTask.setImageDecodeThread(Thread.currentThread());
        ...
    }
...
}
</pre>