summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/html/guide/practices/app-design/jni.jd719
-rw-r--r--docs/html/training/articles/perf-jni.jd7
-rw-r--r--docs/html/training/multiple-threads/communicate-ui.jd263
-rw-r--r--docs/html/training/multiple-threads/create-threadpool.jd238
-rw-r--r--docs/html/training/multiple-threads/define-runnable.jd110
-rw-r--r--docs/html/training/multiple-threads/index.jd83
-rw-r--r--docs/html/training/multiple-threads/run-code.jd148
-rw-r--r--docs/html/training/multiple-threads/threadsample.zipbin0 -> 75204 bytes
-rw-r--r--docs/html/training/training_toc.cs27
-rw-r--r--services/java/com/android/server/InputMethodManagerService.java9
10 files changed, 881 insertions, 723 deletions
diff --git a/docs/html/guide/practices/app-design/jni.jd b/docs/html/guide/practices/app-design/jni.jd
deleted file mode 100644
index ddfa0e3991f5..000000000000
--- a/docs/html/guide/practices/app-design/jni.jd
+++ /dev/null
@@ -1,719 +0,0 @@
-page.title=JNI Tips
-@jd:body
-
-<div id="qv-wrapper">
-<div id="qv">
-
-<h2>In this document</h2>
-<ol>
- <li><a href="#JavaVM_and_JNIEnv">JavaVM and JNIEnv</a></li>
- <li><a href="#threads">Threads</a></li>
- <li><a href="#jclass_jmethodID_and_jfieldID">jclass, jmethodID, and jfieldID</a></li>
- <li><a href="#local_and_global_references">Local and Global References</a></li>
- <li><a href="#UTF_8_and_UTF_16_strings">UTF-8 and UTF-16 Strings</a></li>
- <li><a href="#arrays">Primitive Arrays</a></li>
- <li><a href="#region_calls">Region Calls</a></li>
- <li><a href="#exceptions">Exceptions</a></li>
- <li><a href="#extended_checking">Extended Checking</a> </li>
- <li><a href="#native_libraries">Native Libraries</a></li>
- <li><a href="#64_bit">64-bit Considerations</a></li>
- <li><a href="#unsupported">Unsupported Features/Backwards Compatibility</a></li>
- <li><a href="#faq_ULE">FAQ: Why do I get <code>UnsatisfiedLinkError</code></a></li>
- <li><a href="#faq_FindClass">FAQ: Why didn't <code>FindClass</code> find my class?</a></li>
- <li><a href="#faq_sharing">FAQ: How do I share raw data with native code?</a></li>
-</ol>
-
-</div>
-</div>
-
-<p>JNI is the Java Native Interface. It defines a way for managed code
-(written in the Java programming language) to interact with native
-code (written in C/C++). It's vendor-neutral, has support for loading code from
-dynamic shared libraries, and while cumbersome at times is reasonably efficient.</p>
-
-<p>You really should read through the
-<a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html">JNI spec for J2SE 6</a>
-to get a sense for how JNI works and what features are available. Some
-aspects of the interface aren't immediately obvious on
-first reading, so you may find the next few sections handy.
-There's a more detailed <a href="http://java.sun.com/docs/books/jni/html/jniTOC.html">JNI Programmer's Guide and Specification</a>.</p>
-
-
-<a name="JavaVM_and_JNIEnv" id="JavaVM_and_JNIEnv"></a>
-<h2>JavaVM and JNIEnv</h2>
-
-<p>JNI defines two key data structures, "JavaVM" and "JNIEnv". Both of these are essentially
-pointers to pointers to function tables. (In the C++ version, they're classes with a
-pointer to a function table and a member function for each JNI function that indirects through
-the table.) The JavaVM provides the "invocation interface" functions,
-which allow you to create and destroy a JavaVM. In theory you can have multiple JavaVMs per process,
-but Android only allows one.</p>
-
-<p>The JNIEnv provides most of the JNI functions. Your native functions all receive a JNIEnv as
-the first argument.</p>
-
-<p>The JNIEnv is used for thread-local storage. For this reason, <strong>you cannot share a JNIEnv between threads</strong>.
-If a piece of code has no other way to get its JNIEnv, you should share
-the JavaVM, and use <code>GetEnv</code> to discover the thread's JNIEnv. (Assuming it has one; see <code>AttachCurrentThread</code> below.)</p>
-
-<p>The C declarations of JNIEnv and JavaVM are different from the C++
-declarations. The <code>"jni.h"</code> include file provides different typedefs
-depending on whether it's included into C or C++. For this reason it's a bad idea to
-include JNIEnv arguments in header files included by both languages. (Put another way: if your
-header file requires <code>#ifdef __cplusplus</code>, you may have to do some extra work if anything in
-that header refers to JNIEnv.)</p>
-
-<a name="threads" id="threads"></a>
-<h2>Threads</h2>
-
-<p>All threads are Linux threads, scheduled by the kernel. They're usually
-started from managed code (using <code>Thread.start</code>),
-but they can also be created elsewhere and then attached to the JavaVM. For
-example, a thread started with <code>pthread_create</code> can be attached
-with the JNI <code>AttachCurrentThread</code> or
-<code>AttachCurrentThreadAsDaemon</code> functions. Until a thread is
-attached, it has no JNIEnv, and <strong>cannot make JNI calls</strong>.</p>
-
-<p>Attaching a natively-created thread causes a <code>java.lang.Thread</code>
-object to be constructed and added to the "main" <code>ThreadGroup</code>,
-making it visible to the debugger. Calling <code>AttachCurrentThread</code>
-on an already-attached thread is a no-op.</p>
-
-<p>Android does not suspend threads executing native code. If
-garbage collection is in progress, or the debugger has issued a suspend
-request, Android will pause the thread the next time it makes a JNI call.</p>
-
-<p>Threads attached through JNI <strong>must call
-<code>DetachCurrentThread</code> before they exit</strong>.
-If coding this directly is awkward, in Android 2.0 (Eclair) and higher you
-can use <code>pthread_key_create</code> to define a destructor
-function that will be called before the thread exits, and
-call <code>DetachCurrentThread</code> from there. (Use that
-key with <code>pthread_setspecific</code> to store the JNIEnv in
-thread-local-storage; that way it'll be passed into your destructor as
-the argument.)</p>
-
-
-<a name="jclass_jmethodID_and_jfieldID" id="jclass_jmethodID_and_jfieldID"></a>
-<h2>jclass, jmethodID, and jfieldID</h2>
-
-<p>If you want to access an object's field from native code, you would do the following:</p>
-
-<ul>
-<li> Get the class object reference for the class with <code>FindClass</code></li>
-<li> Get the field ID for the field with <code>GetFieldID</code></li>
-<li> Get the contents of the field with something appropriate, such as
-<code>GetIntField</code></li>
-</ul>
-
-<p>Similarly, to call a method, you'd first get a class object reference and then a method ID. The IDs are often just
-pointers to internal runtime data structures. Looking them up may require several string
-comparisons, but once you have them the actual call to get the field or invoke the method
-is very quick.</p>
-
-<p>If performance is important, it's useful to look the values up once and cache the results
-in your native code. Because there is a limit of one JavaVM per process, it's reasonable
-to store this data in a static local structure.</p>
-
-<p>The class references, field IDs, and method IDs are guaranteed valid until the class is unloaded. Classes
-are only unloaded if all classes associated with a ClassLoader can be garbage collected,
-which is rare but will not be impossible in Android. Note however that
-the <code>jclass</code>
-is a class reference and <strong>must be protected</strong> with a call
-to <code>NewGlobalRef</code> (see the next section).</p>
-
-<p>If you would like to cache the IDs when a class is loaded, and automatically re-cache them
-if the class is ever unloaded and reloaded, the correct way to initialize
-the IDs is to add a piece of code that looks like this to the appropriate class:</p>
-
-<pre> /*
- * We use a class initializer to allow the native code to cache some
- * field offsets. This native function looks up and caches interesting
- * class/field/method IDs. Throws on failure.
- */
- private static native void nativeInit();
-
- static {
- nativeInit();
- }</pre>
-
-<p>Create a <code>nativeClassInit</code> method in your C/C++ code that performs the ID lookups. The code
-will be executed once, when the class is initialized. If the class is ever unloaded and
-then reloaded, it will be executed again.</p>
-
-<a name="local_and_global_references" id="local_and_global_references"></a>
-<h2>Local and Global References</h2>
-
-<p>Every argument passed to a native method, and almost every object returned
-by a JNI function is a "local reference". This means that it's valid for the
-duration of the current native method in the current thread.
-<strong>Even if the object itself continues to live on after the native method
-returns, the reference is not valid.</strong>
-<p>This applies to all sub-classes of <code>jobject</code>, including
-<code>jclass</code>, <code>jstring</code>, and <code>jarray</code>.
-(The runtime will warn you about most reference mis-uses when extended JNI
-checks are enabled.)</p>
-<p>The only way to get non-local references is via the functions
-<code>NewGlobalRef</code> and <code>NewWeakGlobalRef</code>.
-
-<p>If you want to hold on to a reference for a longer period, you must use
-a "global" reference. The <code>NewGlobalRef</code> function takes the
-local reference as an argument and returns a global one.
-The global reference is guaranteed to be valid until you call
-<code>DeleteGlobalRef</code>.</p>
-
-<p>This pattern is commonly used when caching a jclass returned
-from <code>FindClass</code>, e.g.:</p>
-<pre>jclass localClass = env-&gt;FindClass("MyClass");
-jclass globalClass = reinterpret_cast&lt;jclass&gt;(env-&gt;NewGlobalRef(localClass));</pre>
-
-<p>All JNI methods accept both local and global references as arguments.
-It's possible for references to the same object to have different values.
-For example, the return values from consecutive calls to
-<code>NewGlobalRef</code> on the same object may be different.
-<strong>To see if two references refer to the same object,
-you must use the <code>IsSameObject</code> function.</strong> Never compare
-references with <code>==</code> in native code.</p>
-
-<p>One consequence of this is that you
-<strong>must not assume object references are constant or unique</strong>
-in native code. The 32-bit value representing an object may be different
-from one invocation of a method to the next, and it's possible that two
-different objects could have the same 32-bit value on consecutive calls. Do
-not use <code>jobject</code> values as keys.</p>
-
-<p>Programmers are required to "not excessively allocate" local references. In practical terms this means
-that if you're creating large numbers of local references, perhaps while running through an array of
-objects, you should free them manually with
-<code>DeleteLocalRef</code> instead of letting JNI do it for you. The
-implementation is only required to reserve slots for
-16 local references, so if you need more than that you should either delete as you go or use
-<code>EnsureLocalCapacity</code>/<code>PushLocalFrame</code> to reserve more.</p>
-
-<p>Note that <code>jfieldID</code>s and <code>jmethodID</code>s are opaque
-types, not object references, and should not be passed to
-<code>NewGlobalRef</code>. The raw data
-pointers returned by functions like <code>GetStringUTFChars</code>
-and <code>GetByteArrayElements</code> are also not objects. (They may be passed
-between threads, and are valid until the matching Release call.)</p>
-
-<p>One unusual case deserves separate mention. If you attach a native
-thread with <code>AttachCurrentThread</code>, the code you are running will
-never automatically free local references until the thread detaches. Any local
-references you create will have to be deleted manually. In general, any native
-code that creates local references in a loop probably needs to do some manual
-deletion.</p>
-
-<a name="UTF_8_and_UTF_16_strings" id="UTF_8_and_UTF_16_strings"></a>
-<h2>UTF-8 and UTF-16 Strings</h2>
-
-<p>The Java programming language uses UTF-16. For convenience, JNI provides methods that work with <a href="http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">Modified UTF-8</a> as well. The
-modified encoding is useful for C code because it encodes \u0000 as 0xc0 0x80 instead of 0x00.
-The nice thing about this is that you can count on having C-style zero-terminated strings,
-suitable for use with standard libc string functions. The down side is that you cannot pass
-arbitrary UTF-8 data to JNI and expect it to work correctly.</p>
-
-<p>If possible, it's usually faster to operate with UTF-16 strings. Android
-currently does not require a copy in <code>GetStringChars</code>, whereas
-<code>GetStringUTFChars</code> requires an allocation and a conversion to
-UTF-8. Note that
-<strong>UTF-16 strings are not zero-terminated</strong>, and \u0000 is allowed,
-so you need to hang on to the string length as well as
-the jchar pointer.</p>
-
-<p><strong>Don't forget to <code>Release</code> the strings you <code>Get</code></strong>. The
-string functions return <code>jchar*</code> or <code>jbyte*</code>, which
-are C-style pointers to primitive data rather than local references. They
-are guaranteed valid until <code>Release</code> is called, which means they are not
-released when the native method returns.</p>
-
-<p><strong>Data passed to NewStringUTF must be in Modified UTF-8 format</strong>. A
-common mistake is reading character data from a file or network stream
-and handing it to <code>NewStringUTF</code> without filtering it.
-Unless you know the data is 7-bit ASCII, you need to strip out high-ASCII
-characters or convert them to proper Modified UTF-8 form. If you don't,
-the UTF-16 conversion will likely not be what you expect. The extended
-JNI checks will scan strings and warn you about invalid data, but they
-won't catch everything.</p>
-
-<a name="arrays" id="arrays"></a>
-<h2>Primitive Arrays</h2>
-
-<p>JNI provides functions for accessing the contents of array objects.
-While arrays of objects must be accessed one entry at a time, arrays of
-primitives can be read and written directly as if they were declared in C.</p>
-
-<p>To make the interface as efficient as possible without constraining
-the VM implementation, the <code>Get&lt;PrimitiveType&gt;ArrayElements</code>
-family of calls allows the runtime to either return a pointer to the actual elements, or
-allocate some memory and make a copy. Either way, the raw pointer returned
-is guaranteed to be valid until the corresponding <code>Release</code> call
-is issued (which implies that, if the data wasn't copied, the array object
-will be pinned down and can't be relocated as part of compacting the heap).
-<strong>You must <code>Release</code> every array you <code>Get</code>.</strong> Also, if the <code>Get</code>
-call fails, you must ensure that your code doesn't try to <code>Release</code> a NULL
-pointer later.</p>
-
-<p>You can determine whether or not the data was copied by passing in a
-non-NULL pointer for the <code>isCopy</code> argument. This is rarely
-useful.</p>
-
-<p>The <code>Release</code> call takes a <code>mode</code> argument that can
-have one of three values. The actions performed by the runtime depend upon
-whether it returned a pointer to the actual data or a copy of it:</p>
-
-<ul>
- <li><code>0</code>
- <ul>
- <li>Actual: the array object is un-pinned.
- <li>Copy: data is copied back. The buffer with the copy is freed.
- </ul>
- <li><code>JNI_COMMIT</code>
- <ul>
- <li>Actual: does nothing.
- <li>Copy: data is copied back. The buffer with the copy
- <strong>is not freed</strong>.
- </ul>
- <li><code>JNI_ABORT</code>
- <ul>
- <li>Actual: the array object is un-pinned. Earlier
- writes are <strong>not</strong> aborted.
- <li>Copy: the buffer with the copy is freed; any changes to it are lost.
- </ul>
-</ul>
-
-<p>One reason for checking the <code>isCopy</code> flag is to know if
-you need to call <code>Release</code> with <code>JNI_COMMIT</code>
-after making changes to an array &mdash; if you're alternating between making
-changes and executing code that uses the contents of the array, you may be
-able to
-skip the no-op commit. Another possible reason for checking the flag is for
-efficient handling of <code>JNI_ABORT</code>. For example, you might want
-to get an array, modify it in place, pass pieces to other functions, and
-then discard the changes. If you know that JNI is making a new copy for
-you, there's no need to create another "editable" copy. If JNI is passing
-you the original, then you do need to make your own copy.</p>
-
-<p>It is a common mistake (repeated in example code) to assume that you can skip the <code>Release</code> call if
-<code>*isCopy</code> is false. This is not the case. If no copy buffer was
-allocated, then the original memory must be pinned down and can't be moved by
-the garbage collector.</p>
-
-<p>Also note that the <code>JNI_COMMIT</code> flag does <strong>not</strong> release the array,
-and you will need to call <code>Release</code> again with a different flag
-eventually.</p>
-
-
-<a name="region_calls" id="region_calls"></a>
-<h2>Region Calls</h2>
-
-<p>There is an alternative to calls like <code>Get&lt;Type&gt;ArrayElements</code>
-and <code>GetStringChars</code> that may be very helpful when all you want
-to do is copy data in or out. Consider the following:</p>
-
-<pre> jbyte* data = env-&gt;GetByteArrayElements(array, NULL);
- if (data != NULL) {
- memcpy(buffer, data, len);
- env-&gt;ReleaseByteArrayElements(array, data, JNI_ABORT);
- }</pre>
-
-<p>This grabs the array, copies the first <code>len</code> byte
-elements out of it, and then releases the array. Depending upon the
-implementation, the <code>Get</code> call will either pin or copy the array
-contents.
-The code copies the data (for perhaps a second time), then calls <code>Release</code>; in this case
-<code>JNI_ABORT</code> ensures there's no chance of a third copy.</p>
-
-<p>One can accomplish the same thing more simply:</p>
-<pre> env-&gt;GetByteArrayRegion(array, 0, len, buffer);</pre>
-
-<p>This has several advantages:</p>
-<ul>
- <li>Requires one JNI call instead of 2, reducing overhead.
- <li>Doesn't require pinning or extra data copies.
- <li>Reduces the risk of programmer error &mdash; no risk of forgetting
- to call <code>Release</code> after something fails.
-</ul>
-
-<p>Similarly, you can use the <code>Set&lt;Type&gt;ArrayRegion</code> call
-to copy data into an array, and <code>GetStringRegion</code> or
-<code>GetStringUTFRegion</code> to copy characters out of a
-<code>String</code>.
-
-
-<a name="exceptions" id="exceptions"></a>
-<h2>Exceptions</h2>
-
-<p><strong>You must not call most JNI functions while an exception is pending.</strong>
-Your code is expected to notice the exception (via the function's return value,
-<code>ExceptionCheck</code>, or <code>ExceptionOccurred</code>) and return,
-or clear the exception and handle it.</p>
-
-<p>The only JNI functions that you are allowed to call while an exception is
-pending are:</p>
-<ul>
- <li><code>DeleteGlobalRef</code>
- <li><code>DeleteLocalRef</code>
- <li><code>DeleteWeakGlobalRef</code>
- <li><code>ExceptionCheck</code>
- <li><code>ExceptionClear</code>
- <li><code>ExceptionDescribe</code>
- <li><code>ExceptionOccurred</code>
- <li><code>MonitorExit</code>
- <li><code>PopLocalFrame</code>
- <li><code>PushLocalFrame</code>
- <li><code>Release&lt;PrimitiveType&gt;ArrayElements</code>
- <li><code>ReleasePrimitiveArrayCritical</code>
- <li><code>ReleaseStringChars</code>
- <li><code>ReleaseStringCritical</code>
- <li><code>ReleaseStringUTFChars</code>
-</ul>
-
-<p>Many JNI calls can throw an exception, but often provide a simpler way
-of checking for failure. For example, if <code>NewString</code> returns
-a non-NULL value, you don't need to check for an exception. However, if
-you call a method (using a function like <code>CallObjectMethod</code>),
-you must always check for an exception, because the return value is not
-going to be valid if an exception was thrown.</p>
-
-<p>Note that exceptions thrown by interpreted code do not unwind native stack
-frames, and Android does not yet support C++ exceptions.
-The JNI <code>Throw</code> and <code>ThrowNew</code> instructions just
-set an exception pointer in the current thread. Upon returning to managed
-from native code, the exception will be noted and handled appropriately.</p>
-
-<p>Native code can "catch" an exception by calling <code>ExceptionCheck</code> or
-<code>ExceptionOccurred</code>, and clear it with
-<code>ExceptionClear</code>. As usual,
-discarding exceptions without handling them can lead to problems.</p>
-
-<p>There are no built-in functions for manipulating the <code>Throwable</code> object
-itself, so if you want to (say) get the exception string you will need to
-find the <code>Throwable</code> class, look up the method ID for
-<code>getMessage "()Ljava/lang/String;"</code>, invoke it, and if the result
-is non-NULL use <code>GetStringUTFChars</code> to get something you can
-hand to <code>printf(3)</code> or equivalent.</p>
-
-
-<a name="extended_checking" id="extended_checking"></a>
-<h2>Extended Checking</h2>
-
-<p>JNI does very little error checking. Errors usually result in a crash. Android also offers a mode called CheckJNI, where the JavaVM and JNIEnv function table pointers are switched to tables of functions that perform an extended series of checks before calling the standard implementation.</p>
-
-<p>The additional checks include:</p>
-
-<ul>
-<li>Arrays: attempting to allocate a negative-sized array.</li>
-<li>Bad pointers: passing a bad jarray/jclass/jobject/jstring to a JNI call, or passing a NULL pointer to a JNI call with a non-nullable argument.</li>
-<li>Class names: passing anything but the “java/lang/String” style of class name to a JNI call.</li>
-<li>Critical calls: making a JNI call between a “critical” get and its corresponding release.</li>
-<li>Direct ByteBuffers: passing bad arguments to <code>NewDirectByteBuffer</code>.</li>
-<li>Exceptions: making a JNI call while there’s an exception pending.</li>
-<li>JNIEnv*s: using a JNIEnv* from the wrong thread.</li>
-<li>jfieldIDs: using a NULL jfieldID, or using a jfieldID to set a field to a value of the wrong type (trying to assign a StringBuilder to a String field, say), or using a jfieldID for a static field to set an instance field or vice versa, or using a jfieldID from one class with instances of another class.</li>
-<li>jmethodIDs: using the wrong kind of jmethodID when making a <code>Call*Method</code> JNI call: incorrect return type, static/non-static mismatch, wrong type for ‘this’ (for non-static calls) or wrong class (for static calls).</li>
-<li>References: using <code>DeleteGlobalRef</code>/<code>DeleteLocalRef</code> on the wrong kind of reference.</li>
-<li>Release modes: passing a bad release mode to a release call (something other than <code>0</code>, <code>JNI_ABORT</code>, or <code>JNI_COMMIT</code>).</li>
-<li>Type safety: returning an incompatible type from your native method (returning a StringBuilder from a method declared to return a String, say).</li>
-<li>UTF-8: passing an invalid <a href="http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">Modified UTF-8</a> byte sequence to a JNI call.</li>
-</ul>
-
-<p>(Accessibility of methods and fields is still not checked: access restrictions don't apply to native code.)</p>
-
-<p>There are several ways to enable CheckJNI.</p>
-
-<p>If you’re using the emulator, CheckJNI is on by default.</p>
-
-<p>If you have a rooted device, you can use the following sequence of commands to restart the runtime with CheckJNI enabled:</p>
-
-<pre>adb shell stop
-adb shell setprop dalvik.vm.checkjni true
-adb shell start</pre>
-
-<p>In either of these cases, you’ll see something like this in your logcat output when the runtime starts:</p>
-
-<pre>D AndroidRuntime: CheckJNI is ON</pre>
-
-<p>If you have a regular device, you can use the following command:</p>
-
-<pre>adb shell setprop debug.checkjni 1</pre>
-
-<p>This won’t affect already-running apps, but any app launched from that point on will have CheckJNI enabled. (Change the property to any other value or simply rebooting will disable CheckJNI again.) In this case, you’ll see something like this in your logcat output the next time an app starts:</p>
-
-<pre>D Late-enabling CheckJNI</pre>
-
-
-
-
-<a name="native_libraries" id="native_libraries"></a>
-<h2>Native Libraries</h2>
-
-<p>You can load native code from shared libraries with the standard
-<code>System.loadLibrary</code> call. The
-preferred way to get at your native code is:</p>
-
-<ul>
-<li> Call <code>System.loadLibrary</code> from a static class
-initializer. (See the earlier example, where one is used to call
-<code>nativeClassInit</code>.) The argument is the "undecorated"
-library name, so to load "libfubar.so" you would pass in "fubar".</li>
-<li> Provide a native function: <code><strong>jint JNI_OnLoad(JavaVM* vm, void* reserved)</strong></code></li>
-<li>In <code>JNI_OnLoad</code>, register all of your native methods. You
-should declare
-the methods "static" so the names don't take up space in the symbol table
-on the device.</li>
-</ul>
-
-<p>The <code>JNI_OnLoad</code> function should look something like this if
-written in C++:</p>
-<pre>jint JNI_OnLoad(JavaVM* vm, void* reserved)
-{
- JNIEnv* env;
- if (vm-&gt;GetEnv(reinterpret_cast&lt;void**&gt;(&env), JNI_VERSION_1_6) != JNI_OK) {
- return -1;
- }
-
- // Get jclass with env-&gt;FindClass.
- // Register methods with env-&gt;RegisterNatives.
-
- return JNI_VERSION_1_6;
-}</pre>
-
-<p>You can also call <code>System.load</code> with the full path name of the
-shared library. For Android apps, you may find it useful to get the full
-path to the application's private data storage area from the context object.</p>
-
-<p>This is the recommended approach, but not the only approach. Explicit
-registration is not required, nor is it necessary that you provide a
-<code>JNI_OnLoad</code> function.
-You can instead use "discovery" of native methods that are named in a
-specific way (see <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp615">the JNI spec</a> for details), though this is less desirable because if a method signature is wrong you won't know
-about it until the first time the method is actually used.</p>
-
-<p>One other note about <code>JNI_OnLoad</code>: any <code>FindClass</code>
-calls you make from there will happen in the context of the class loader
-that was used to load the shared library. Normally <code>FindClass</code>
-uses the loader associated with the method at the top of the interpreted
-stack, or if there isn't one (because the thread was just attached) it uses
-the "system" class loader. This makes
-<code>JNI_OnLoad</code> a convenient place to look up and cache class
-object references.</p>
-
-
-<a name="64_bit" id="64_bit"></a>
-<h2>64-bit Considerations</h2>
-
-<p>Android is currently expected to run on 32-bit platforms. In theory it
-could be built for a 64-bit system, but that is not a goal at this time.
-For the most part this isn't something that you will need to worry about
-when interacting with native code,
-but it becomes significant if you plan to store pointers to native
-structures in integer fields in an object. To support architectures
-that use 64-bit pointers, <strong>you need to stash your native pointers in a
-<code>long</code> field rather than an <code>int</code></strong>.
-
-
-<a name="unsupported" id="unsupported"></a>
-<h2>Unsupported Features/Backwards Compatibility</h2>
-
-<p>All JNI 1.6 features are supported, with the following exception:</p>
-<ul>
- <li><code>DefineClass</code> is not implemented. Android does not use
- Java bytecodes or class files, so passing in binary class data
- doesn't work.</li>
-</ul>
-
-<p>For backward compatibility with older Android releases, you may need to
-be aware of:</p>
-<ul>
- <li><b>Dynamic lookup of native functions</b>
- <p>Until Android 2.0 (Eclair), the '$' character was not properly
- converted to "_00024" during searches for method names. Working
- around this requires using explicit registration or moving the
- native methods out of inner classes.
- <li><b>Detaching threads</b>
- <p>Until Android 2.0 (Eclair), it was not possible to use a <code>pthread_key_create</code>
- destructor function to avoid the "thread must be detached before
- exit" check. (The runtime also uses a pthread key destructor function,
- so it'd be a race to see which gets called first.)
- <li><b>Weak global references</b>
- <p>Until Android 2.2 (Froyo), weak global references were not implemented.
- Older versions will vigorously reject attempts to use them. You can use
- the Android platform version constants to test for support.
- <p>Until Android 4.0 (Ice Cream Sandwich), weak global references could only
- be passed to <code>NewLocalRef</code>, <code>NewGlobalRef</code>, and
- <code>DeleteWeakGlobalRef</code>. (The spec strongly encourages
- programmers to create hard references to weak globals before doing
- anything with them, so this should not be at all limiting.)
- <p>From Android 4.0 (Ice Cream Sandwich) on, weak global references can be
- used like any other JNI references.</li>
- <li><b>Local references</b>
- <p>Until Android 4.0 (Ice Cream Sandwich), local references were
- actually direct pointers. Ice Cream Sandwich added the indirection
- necessary to support better garbage collectors, but this means that lots
- of JNI bugs are undetectable on older releases. See
- <a href="http://android-developers.blogspot.com/2011/11/jni-local-reference-changes-in-ics.html">JNI Local Reference Changes in ICS</a> for more details.
- <li><b>Determining reference type with <code>GetObjectRefType</code></b>
- <p>Until Android 4.0 (Ice Cream Sandwich), as a consequence of the use of
- direct pointers (see above), it was impossible to implement
- <code>GetObjectRefType</code> correctly. Instead we used a heuristic
- that looked through the weak globals table, the arguments, the locals
- table, and the globals table in that order. The first time it found your
- direct pointer, it would report that your reference was of the type it
- happened to be examining. This meant, for example, that if
- you called <code>GetObjectRefType</code> on a global jclass that happened
- to be the same as the jclass passed as an implicit argument to your static
- native method, you'd get <code>JNILocalRefType</code> rather than
- <code>JNIGlobalRefType</code>.
-</ul>
-
-
-<a name="faq_ULE" id="faq_ULE"></a>
-<h2>FAQ: Why do I get <code>UnsatisfiedLinkError</code>?</h2>
-
-<p>When working on native code it's not uncommon to see a failure like this:</p>
-<pre>java.lang.UnsatisfiedLinkError: Library foo not found</pre>
-
-<p>In some cases it means what it says &mdash; the library wasn't found. In
-other cases the library exists but couldn't be opened by <code>dlopen(3)</code>, and
-the details of the failure can be found in the exception's detail message.</p>
-
-<p>Common reasons why you might encounter "library not found" exceptions:</p>
-<ul>
- <li>The library doesn't exist or isn't accessible to the app. Use
- <code>adb shell ls -l &lt;path&gt;</code> to check its presence
- and permissions.
- <li>The library wasn't built with the NDK. This can result in
- dependencies on functions or libraries that don't exist on the device.
-</ul>
-
-<p>Another class of <code>UnsatisfiedLinkError</code> failures looks like:</p>
-<pre>java.lang.UnsatisfiedLinkError: myfunc
- at Foo.myfunc(Native Method)
- at Foo.main(Foo.java:10)</pre>
-
-<p>In logcat, you'll see:</p>
-<pre>W/dalvikvm( 880): No implementation found for native LFoo;.myfunc ()V</pre>
-
-<p>This means that the runtime tried to find a matching method but was
-unsuccessful. Some common reasons for this are:</p>
-<ul>
- <li>The library isn't getting loaded. Check the logcat output for
- messages about library loading.
- <li>The method isn't being found due to a name or signature mismatch. This
- is commonly caused by:
- <ul>
- <li>For lazy method lookup, failing to declare C++ functions
- with <code>extern "C"</code> and appropriate
- visibility (<code>JNIEXPORT</code>). Note that prior to Ice Cream
- Sandwich, the JNIEXPORT macro was incorrect, so using a new GCC with
- an old <code>jni.h</code> won't work.
- You can use <code>arm-eabi-nm</code>
- to see the symbols as they appear in the library; if they look
- mangled (something like <code>_Z15Java_Foo_myfuncP7_JNIEnvP7_jclass</code>
- rather than <code>Java_Foo_myfunc</code>), or if the symbol type is
- a lowercase 't' rather than an uppercase 'T', then you need to
- adjust the declaration.
- <li>For explicit registration, minor errors when entering the
- method signature. Make sure that what you're passing to the
- registration call matches the signature in the log file.
- Remember that 'B' is <code>byte</code> and 'Z' is <code>boolean</code>.
- Class name components in signatures start with 'L', end with ';',
- use '/' to separate package/class names, and use '$' to separate
- inner-class names (<code>Ljava/util/Map$Entry;</code>, say).
- </ul>
-</ul>
-
-<p>Using <code>javah</code> to automatically generate JNI headers may help
-avoid some problems.
-
-
-<a name="faq_FindClass" id="faq_FindClass"></a>
-<h2>FAQ: Why didn't <code>FindClass</code> find my class?</h2>
-
-<p>Make sure that the class name string has the correct format. JNI class
-names start with the package name and are separated with slashes,
-such as <code>java/lang/String</code>. If you're looking up an array class,
-you need to start with the appropriate number of square brackets and
-must also wrap the class with 'L' and ';', so a one-dimensional array of
-<code>String</code> would be <code>[Ljava/lang/String;</code>.</p>
-
-<p>If the class name looks right, you could be running into a class loader
-issue. <code>FindClass</code> wants to start the class search in the
-class loader associated with your code. It examines the call stack,
-which will look something like:
-<pre> Foo.myfunc(Native Method)
- Foo.main(Foo.java:10)
- dalvik.system.NativeStart.main(Native Method)</pre>
-
-<p>The topmost method is <code>Foo.myfunc</code>. <code>FindClass</code>
-finds the <code>ClassLoader</code> object associated with the <code>Foo</code>
-class and uses that.</p>
-
-<p>This usually does what you want. You can get into trouble if you
-create a thread yourself (perhaps by calling <code>pthread_create</code>
-and then attaching it with <code>AttachCurrentThread</code>).
-Now the stack trace looks like this:</p>
-<pre> dalvik.system.NativeStart.run(Native Method)</pre>
-
-<p>The topmost method is <code>NativeStart.run</code>, which isn't part of
-your application. If you call <code>FindClass</code> from this thread, the
-JavaVM will start in the "system" class loader instead of the one associated
-with your application, so attempts to find app-specific classes will fail.</p>
-
-<p>There are a few ways to work around this:</p>
-<ul>
- <li>Do your <code>FindClass</code> lookups once, in
- <code>JNI_OnLoad</code>, and cache the class references for later
- use. Any <code>FindClass</code> calls made as part of executing
- <code>JNI_OnLoad</code> will use the class loader associated with
- the function that called <code>System.loadLibrary</code> (this is a
- special rule, provided to make library initialization more convenient).
- If your app code is loading the library, <code>FindClass</code>
- will use the correct class loader.
- <li>Pass an instance of the class into the functions that need
- it, by declaring your native method to take a Class argument and
- then passing <code>Foo.class</code> in.
- <li>Cache a reference to the <code>ClassLoader</code> object somewhere
- handy, and issue <code>loadClass</code> calls directly. This requires
- some effort.
-</ul>
-
-
-<a name="faq_sharing" id="faq_sharing"></a>
-<h2>FAQ: How do I share raw data with native code?</h2>
-
-<p>You may find yourself in a situation where you need to access a large
-buffer of raw data from both managed and native code. Common examples
-include manipulation of bitmaps or sound samples. There are two
-basic approaches.</p>
-
-<p>You can store the data in a <code>byte[]</code>. This allows very fast
-access from managed code. On the native side, however, you're
-not guaranteed to be able to access the data without having to copy it. In
-some implementations, <code>GetByteArrayElements</code> and
-<code>GetPrimitiveArrayCritical</code> will return actual pointers to the
-raw data in the managed heap, but in others it will allocate a buffer
-on the native heap and copy the data over.</p>
-
-<p>The alternative is to store the data in a direct byte buffer. These
-can be created with <code>java.nio.ByteBuffer.allocateDirect</code>, or
-the JNI <code>NewDirectByteBuffer</code> function. Unlike regular
-byte buffers, the storage is not allocated on the managed heap, and can
-always be accessed directly from native code (get the address
-with <code>GetDirectBufferAddress</code>). Depending on how direct
-byte buffer access is implemented, accessing the data from managed code
-can be very slow.</p>
-
-<p>The choice of which to use depends on two factors:</p>
-<ol>
- <li>Will most of the data accesses happen from code written in Java
- or in C/C++?
- <li>If the data is eventually being passed to a system API, what form
- must it be in? (For example, if the data is eventually passed to a
- function that takes a byte[], doing processing in a direct
- <code>ByteBuffer</code> might be unwise.)
-</ol>
-
-<p>If there's no clear winner, use a direct byte buffer. Support for them
-is built directly into JNI, and performance should improve in future releases.</p>
diff --git a/docs/html/training/articles/perf-jni.jd b/docs/html/training/articles/perf-jni.jd
index a21e9fea41e4..2abb000c05e3 100644
--- a/docs/html/training/articles/perf-jni.jd
+++ b/docs/html/training/articles/perf-jni.jd
@@ -32,12 +32,11 @@ page.article=true
code (written in C/C++). It's vendor-neutral, has support for loading code from
dynamic shared libraries, and while cumbersome at times is reasonably efficient.</p>
-<p>You really should read through the
-<a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html">JNI spec for J2SE 6</a>
+<p>If you're not already familiar with it, read through the
+<a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html">Java Native Interface Specification</a>
to get a sense for how JNI works and what features are available. Some
aspects of the interface aren't immediately obvious on
-first reading, so you may find the next few sections handy.
-There's a more detailed <a href="http://java.sun.com/docs/books/jni/html/jniTOC.html">JNI Programmer's Guide and Specification</a>.</p>
+first reading, so you may find the next few sections handy.</p>
<a name="JavaVM_and_JNIEnv" id="JavaVM_and_JNIEnv"></a>
diff --git a/docs/html/training/multiple-threads/communicate-ui.jd b/docs/html/training/multiple-threads/communicate-ui.jd
new file mode 100644
index 000000000000..d9977d34c9e0
--- /dev/null
+++ b/docs/html/training/multiple-threads/communicate-ui.jd
@@ -0,0 +1,263 @@
+page.title=Communicating with the UI 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="#Handler">Define a Handler on the UI Thread</a></li>
+ <li><a href="#MoveValues">Move Data from a Task to the UI Thread</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>
+<p>
+ In the previous lesson you learned how to start a task on a thread managed by
+ {@link java.util.concurrent.ThreadPoolExecutor}. This final lesson shows you how to send data
+ from the task to objects running on the user interface (UI) thread. This feature allows your
+ tasks to do background work and then move the results to UI elements such as bitmaps.
+</p>
+<p>
+ Every app has its own special thread that runs UI objects such as {@link android.view.View}
+ objects; this thread is called the UI thread. Only objects running on the UI thread have access
+ to other objects on that thread. Because tasks that you run on a thread from a thread pool
+ <em>aren't</em> running on your UI thread, they don't have access to UI objects. To move data
+ from a background thread to the UI thread, use a {@link android.os.Handler} that's
+ running on the UI thread.
+</p>
+<h2 id="Handler">Define a Handler on the UI Thread</h2>
+<p>
+ {@link android.os.Handler} is part of the Android system's framework for managing threads. A
+ {@link android.os.Handler} object receives messages and runs code to handle the messages.
+ Normally, you create a {@link android.os.Handler} for a new thread, but you can
+ also create a {@link android.os.Handler} that's connected to an existing thread.
+ When you connect a {@link android.os.Handler} to your UI thread, the code that handles messages
+ runs on the UI thread.
+</p>
+<p>
+ Instantiate the {@link android.os.Handler} object in the constructor for the class that
+ creates your thread pools, and store the object in a global variable. Connect it to the UI
+ thread by instantiating it with the {@link android.os.Handler#Handler(Looper) Handler(Looper)}
+ constructor. This constructor uses a {@link android.os.Looper} object, which is another part of
+ the Android system's thread management framework. When you instantiate a
+ {@link android.os.Handler} based on a particular {@link android.os.Looper} instance, the
+ {@link android.os.Handler} runs on the same thread as the {@link android.os.Looper}.
+ For example:
+</p>
+<pre>
+private PhotoManager() {
+...
+ // Defines a Handler object that's attached to the UI thread
+ mHandler = new Handler(Looper.getMainLooper()) {
+ ...
+</pre>
+<p>
+ Inside the {@link android.os.Handler}, override the {@link android.os.Handler#handleMessage
+ handleMessage()} method. The Android system invokes this method when it receives a new message
+ for a thread it's managing; all of the {@link android.os.Handler} objects for a particular
+ thread receive the same message. For example:
+</p>
+<pre>
+ /*
+ * handleMessage() defines the operations to perform when
+ * the Handler receives a new Message to process.
+ */
+ &#64;Override
+ public void handleMessage(Message inputMessage) {
+ // Gets the image task from the incoming Message object.
+ PhotoTask photoTask = (PhotoTask) inputMessage.obj;
+ ...
+ }
+ ...
+ }
+}
+The next section shows how to tell the {@link android.os.Handler} to move data.
+</pre>
+<h2 id="MoveValues">Move Data from a Task to the UI Thread</h2>
+<p>
+ To move data from a task object running on a background thread to an object on the UI thread,
+ start by storing references to the data and the UI object in the task object. Next, pass the
+ task object and a status code to the object that instantiated the {@link android.os.Handler}.
+ In this object, send a {@link android.os.Message} containing the status and the task object to
+ the {@link android.os.Handler}. Because {@link android.os.Handler} is running on the UI thread,
+ it can move the data to the UI object.
+
+<h3>Store data in the task object</h3>
+<p>
+ For example, here's a {@link java.lang.Runnable}, running on a background thread, that decodes a
+ {@link android.graphics.Bitmap} and stores it in its parent object <code>PhotoTask</code>.
+ The {@link java.lang.Runnable} also stores the status code <code>DECODE_STATE_COMPLETED</code>.
+</p>
+<pre>
+// A class that decodes photo files into Bitmaps
+class PhotoDecodeRunnable implements Runnable {
+ ...
+ PhotoDecodeRunnable(PhotoTask downloadTask) {
+ mPhotoTask = downloadTask;
+ }
+ ...
+ // Gets the downloaded byte array
+ byte[] imageBuffer = mPhotoTask.getByteBuffer();
+ ...
+ // Runs the code for this task
+ public void run() {
+ ...
+ // Tries to decode the image buffer
+ returnBitmap = BitmapFactory.decodeByteArray(
+ imageBuffer,
+ 0,
+ imageBuffer.length,
+ bitmapOptions
+ );
+ ...
+ // Sets the ImageView Bitmap
+ mPhotoTask.setImage(returnBitmap);
+ // Reports a status of "completed"
+ mPhotoTask.handleDecodeState(DECODE_STATE_COMPLETED);
+ ...
+ }
+ ...
+}
+...
+</pre>
+<p>
+ <code>PhotoTask</code> also contains a handle to the {@link android.widget.ImageView} that
+ displays the {@link android.graphics.Bitmap}. Even though references to
+ the {@link android.graphics.Bitmap} and {@link android.widget.ImageView} are in the same object,
+ you can't assign the {@link android.graphics.Bitmap} to the {@link android.widget.ImageView},
+ because you're not currently running on the UI thread.
+</p>
+<p>
+ Instead, the next step is to send this status to the <code>PhotoTask</code> object.
+</p>
+<h3>Send status up the object hierarchy</h3>
+<p>
+ <code>PhotoTask</code> is the next higher object in the hierarchy. It maintains references to
+ the decoded data and the {@link android.view.View} object that will show the data. It receives
+ a status code from <code>PhotoDecodeRunnable</code> and passes it along to the object that
+ maintains thread pools and instantiates {@link android.os.Handler}:
+</p>
+<pre>
+public class PhotoTask {
+ ...
+ // Gets a handle to the object that creates the thread pools
+ sPhotoManager = PhotoManager.getInstance();
+ ...
+ public void handleDecodeState(int state) {
+ int outState;
+ // Converts the decode state to the overall state.
+ switch(state) {
+ case PhotoDecodeRunnable.DECODE_STATE_COMPLETED:
+ outState = PhotoManager.TASK_COMPLETE;
+ break;
+ ...
+ }
+ ...
+ // Calls the generalized state method
+ handleState(outState);
+ }
+ ...
+ // Passes the state to PhotoManager
+ void handleState(int state) {
+ /*
+ * Passes a handle to this task and the
+ * current state to the class that created
+ * the thread pools
+ */
+ sPhotoManager.handleState(this, state);
+ }
+ ...
+}
+</pre>
+<h3>Move data to the UI</h3>
+<p>
+ From the <code>PhotoTask</code> object, the <code>PhotoManager</code> object receives a status
+ code and a handle to the <code>PhotoTask</code> object. Because the status is
+ <code>TASK_COMPLETE</code>, creates a {@link android.os.Message} containing the state and task
+ object and sends it to the {@link android.os.Handler}:
+</p>
+<pre>
+public class PhotoManager {
+ ...
+ // Handle status messages from tasks
+ public void handleState(PhotoTask photoTask, int state) {
+ switch (state) {
+ ...
+ // The task finished downloading and decoding the image
+ case TASK_COMPLETE:
+ /*
+ * Creates a message for the Handler
+ * with the state and the task object
+ */
+ Message completeMessage =
+ mHandler.obtainMessage(state, photoTask);
+ completeMessage.sendToTarget();
+ break;
+ ...
+ }
+ ...
+ }
+</pre>
+<p>
+ Finally, {@link android.os.Handler#handleMessage Handler.handleMessage()} checks the status
+ code for each incoming {@link android.os.Message}. If the status code is
+ <code>TASK_COMPLETE</code>, then the task is finished, and the <code>PhotoTask</code> object
+ in the {@link android.os.Message} contains both a {@link android.graphics.Bitmap} and an
+ {@link android.widget.ImageView}. Because
+ {@link android.os.Handler#handleMessage Handler.handleMessage()} is
+ running on the UI thread, it can safely move the {@link android.graphics.Bitmap} to the
+ {@link android.widget.ImageView}:
+</p>
+<pre>
+ private PhotoManager() {
+ ...
+ mHandler = new Handler(Looper.getMainLooper()) {
+ &#64;Override
+ public void handleMessage(Message inputMessage) {
+ // Gets the task from the incoming Message object.
+ PhotoTask photoTask = (PhotoTask) inputMessage.obj;
+ // Gets the ImageView for this task
+ PhotoView localView = photoTask.getPhotoView();
+ ...
+ switch (inputMessage.what) {
+ ...
+ // The decoding is done
+ case TASK_COMPLETE:
+ /*
+ * Moves the Bitmap from the task
+ * to the View
+ */
+ localView.setImageBitmap(photoTask.getImage());
+ break;
+ ...
+ default:
+ /*
+ * Pass along other messages from the UI
+ */
+ super.handleMessage(inputMessage);
+ }
+ ...
+ }
+ ...
+ }
+ ...
+ }
+...
+}
+</pre>
diff --git a/docs/html/training/multiple-threads/create-threadpool.jd b/docs/html/training/multiple-threads/create-threadpool.jd
new file mode 100644
index 000000000000..4a4ddb1255f8
--- /dev/null
+++ b/docs/html/training/multiple-threads/create-threadpool.jd
@@ -0,0 +1,238 @@
+page.title=Creating a Manager for Multiple Threads
+
+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="#ClassStructure">Define the Thread Pool Class</a>
+ <li><a href="#PoolParameters">Determine the Thread Pool Parameters</a></li>
+ <li><a href="#ThreadPool">Create a Pool of Threads</a></li>
+</ol>
+
+<!-- other docs (NOT javadocs) -->
+<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>
+
+<p>
+ The previous lesson showed how to define a task that executes on a
+ separate thread. If you only want to run the task once, this may be all you need. If you want
+ to run a task repeatedly on different sets of data, but you only need one execution running at a
+ time, an {@link android.app.IntentService} suits your needs. To automatically run tasks
+ as resources become available, or to allow multiple tasks to run at the same time (or both),
+ you need to provide a managed collection of threads. To do this, use an instance of
+ {@link java.util.concurrent.ThreadPoolExecutor}, which runs a task from a queue when a thread
+ in its pool becomes free. To run a task, all you have to do is add it to the queue.
+</p>
+<p>
+ A thread pool can run multiple parallel instances of a task, so you should ensure that your
+ code is thread-safe. Enclose variables that can be accessed by more than one thread in a
+ <code>synchronized</code> block. This approach will prevent one thread from reading the variable
+ while another is writing to it. Typically, this situation arises with static variables, but it
+ also occurs in any object that is only instantiated once. To learn more about this, read the
+ <a href="{@docRoot}http://developer.android.com/guide/components/processes-and-threads.html">
+ Processes and Threads</a> API guide.
+
+</p>
+<h2 id="ClassStructure">Define the Thread Pool Class</h2>
+<p>
+ Instantiate {@link java.util.concurrent.ThreadPoolExecutor} in its own class. Within this class,
+ do the following:
+</p>
+<dl>
+ <dt>
+ Use static variables for thread pools
+ </dt>
+ <dd>
+ You may only want a single instance of a thread pool for your app, in order to have a
+ single control point for restricted CPU or network resources. If you have different
+ {@link java.lang.Runnable} types, you may want to have a thread pool for each one, but each
+ of these can be a single instance. For example, you can add this as part of your
+ global field declarations:
+<pre>
+public class PhotoManager {
+ ...
+ static {
+ ...
+ // Creates a single static instance of PhotoManager
+ sInstance = new PhotoManager();
+ }
+ ...
+</pre>
+ </dd>
+ <dt>
+ Use a private constructor
+ </dt>
+ <dd>
+ Making the constructor private ensures that it is a singleton, which means that you don't
+ have to enclose accesses to the class in a <code>synchronized</code> block:
+<pre>
+public class PhotoManager {
+ ...
+ /**
+ * Constructs the work queues and thread pools used to download
+ * and decode images. Because the constructor is marked private,
+ * it's unavailable to other classes, even in the same package.
+ */
+ private PhotoManager() {
+ ...
+ }
+</pre>
+ </dd>
+ <dt>
+ Start your tasks by calling methods in the thread pool class.
+ </dt>
+ <dd>
+ Define a method in the thread pool class that adds a task to a thread pool's queue. For
+ example:
+<pre>
+public class PhotoManager {
+ ...
+ // Called by the PhotoView to get a photo
+ static public PhotoTask startDownload(
+ PhotoView imageView,
+ boolean cacheFlag) {
+ ...
+ // Adds a download task to the thread pool for execution
+ sInstance.
+ mDownloadThreadPool.
+ execute(downloadTask.getHTTPDownloadRunnable());
+ ...
+ }
+</pre>
+ </dd>
+ <dt>
+ Instantiate a {@link android.os.Handler} in the constructor and attach it to your app's
+ UI thread.
+ </dt>
+ <dd>
+ A {@link android.os.Handler} allows your app to safely call the methods of UI objects
+ such as {@link android.view.View} objects. Most UI objects may only be safely altered from
+ the UI thread. This approach is described in more detail in the lesson
+ <a href="communicate-ui.html">Communicate with the UI Thread</a>. For example:
+<pre>
+ private PhotoManager() {
+ ...
+ // Defines a Handler object that's attached to the UI thread
+ mHandler = new Handler(Looper.getMainLooper()) {
+ /*
+ * handleMessage() defines the operations to perform when
+ * the Handler receives a new Message to process.
+ */
+ &#64;Override
+ public void handleMessage(Message inputMessage) {
+ ...
+ }
+ ...
+ }
+ }
+</pre>
+ </dd>
+</dl>
+<h2 id="PoolParameters">Determine the Thread Pool Parameters</h2>
+<p>
+ Once you have the overall class structure, you can start defining the thread pool. To
+ instantiate a {@link java.util.concurrent.ThreadPoolExecutor} object, you need the
+ following values:
+</p>
+<dl>
+ <dt>
+ Initial pool size and maximum pool size
+ </dt>
+ <dd>
+ The initial number of threads to allocate to the pool, and the maximum allowable number.
+ The number of threads you can have in a thread pool depends primarily on the number of cores
+ available for your device. This number is available from the system environment:
+<pre>
+public class PhotoManager {
+...
+ /*
+ * Gets the number of available cores
+ * (not always the same as the maximum number of cores)
+ */
+ private static int NUMBER_OF_CORES =
+ Runtime.getRuntime().availableProcessors();
+}
+</pre>
+ This number may not reflect the number of physical cores in the device; some devices have
+ CPUs that deactivate one or more cores depending on the system load. For these devices,
+ {@link java.lang.Runtime#availableProcessors availableProcessors()} returns the number of
+ <i>active</i> cores, which may be less than the total number of cores.
+ </dd>
+ <dt>
+ Keep alive time and time unit
+ </dt>
+ <dd>
+ The duration that a thread will remain idle before it shuts down. The duration is
+ interpreted by the time unit value, one of the constants defined in
+ {@link java.util.concurrent.TimeUnit}.
+ </dd>
+ <dt>
+ A queue of tasks
+ </dt>
+ <dd>
+ The incoming queue from which {@link java.util.concurrent.ThreadPoolExecutor} takes
+ {@link java.lang.Runnable} objects. To start code on a thread, a thread pool manager takes a
+ {@link java.lang.Runnable} object from a first-in, first-out queue and attaches it to the
+ thread. You provide this queue object when you create the thread pool, using any queue class
+ that implements the {@link java.util.concurrent.BlockingQueue} interface. To match the
+ requirements of your app, you can choose from the available queue implementations; to learn
+ more about them, see the class overview for {@link java.util.concurrent.ThreadPoolExecutor}.
+ This example uses the {@link java.util.concurrent.LinkedBlockingQueue} class:
+<pre>
+public class PhotoManager {
+ ...
+ private PhotoManager() {
+ ...
+ // A queue of Runnables
+ private final BlockingQueue&lt;Runnable&gt; mDecodeWorkQueue;
+ ...
+ // Instantiates the queue of Runnables as a LinkedBlockingQueue
+ mDecodeWorkQueue = new LinkedBlockingQueue&lt;Runnable&gt;();
+ ...
+ }
+ ...
+}
+</pre>
+ </dd>
+</dl>
+<h2 id="ThreadPool">Create a Pool of Threads</h2>
+<p>
+ To create a pool of threads, instantiate a thread pool manager by calling
+ {@link java.util.concurrent.ThreadPoolExecutor#ThreadPoolExecutor ThreadPoolExecutor()}.
+ This creates and manages a constrained group of threads. Because the initial pool size and
+ the maximum pool size are the same, {@link java.util.concurrent.ThreadPoolExecutor} creates
+ all of the thread objects when it is instantiated. For example:
+</p>
+<pre>
+ private PhotoManager() {
+ ...
+ // Sets the amount of time an idle thread waits before terminating
+ private static final int KEEP_ALIVE_TIME = 1;
+ // Sets the Time Unit to seconds
+ private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
+ // Creates a thread pool manager
+ mDecodeThreadPool = new ThreadPoolExecutor(
+ NUMBER_OF_CORES, // Initial pool size
+ NUMBER_OF_CORES, // Max pool size
+ KEEP_ALIVE_TIME,
+ KEEP_ALIVE_TIME_UNIT,
+ mDecodeWorkQueue);
+ }
+</pre>
diff --git a/docs/html/training/multiple-threads/define-runnable.jd b/docs/html/training/multiple-threads/define-runnable.jd
new file mode 100644
index 000000000000..17640a976a98
--- /dev/null
+++ b/docs/html/training/multiple-threads/define-runnable.jd
@@ -0,0 +1,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 the PhotoTask instance,
+ * so that the instance
+ * can interrupt the Thread.
+ */
+ mPhotoTask.setImageDecodeThread(Thread.currentThread());
+ ...
+ }
+...
+}
+</pre>
diff --git a/docs/html/training/multiple-threads/index.jd b/docs/html/training/multiple-threads/index.jd
new file mode 100644
index 000000000000..3ea57c51b05c
--- /dev/null
+++ b/docs/html/training/multiple-threads/index.jd
@@ -0,0 +1,83 @@
+page.title=Sending Operations to Multiple Threads
+
+trainingnavtop=true
+startpage=true
+
+
+@jd:body
+
+<div id="tb-wrapper">
+<div id="tb">
+
+<!-- Required platform, tools, add-ons, devices, knowledge, etc. -->
+<h2>Dependencies and prerequisites</h2>
+<ul>
+ <li>Android 3.0 (API Level 11) or higher</li>
+ <li>
+ <a href="{@docRoot}training/load-data-background/index.html">
+ Loading Data in the Background</a> training class
+ </li>
+ <li>
+ <a href="{@docRoot}training/run-background-service/index.html">
+ Running in a Background Service</a> training class
+ </li>
+</ul>
+
+<!-- related docs (NOT javadocs) -->
+<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>
+<p>
+ The speed and efficiency of a long-running, data-intensive operation often improves when you
+ split it into smaller operations running on multiple threads. On a device that has a CPU with
+ multiple processors (cores), the system can run the threads in parallel, rather than making each
+ sub-operation wait for a chance to run. For example, decoding multiple image files in order to
+ display them on a thumbnail screen runs substantially faster when you do each decode on a
+ separate thread.
+</p>
+<p>
+ This class shows you how to set up and use multiple threads in an Android app, using a
+ thread pool object. You'll also learn how to define code to run on a thread and how to
+ communicate between one of these threads and the UI thread.
+</p>
+<h2>Lessons</h2>
+<dl>
+ <dt>
+ <b><a href="define-runnable.html">Specifying the Code to Run on a Thread</a></b>
+ </dt>
+ <dd>
+ Learn how to write code to run on a separate {@link java.lang.Thread}, by
+ defining a class that implements the {@link java.lang.Runnable} interface.
+ </dd>
+ <dt>
+ <b><a href="create-threadpool.html">Creating a Manager for Multiple Threads</a></b>
+ </dt>
+ <dd>
+ Learn how to create an object that manages a pool of {@link java.lang.Thread} objects and
+ a queue of {@link java.lang.Runnable} objects. This object is called a
+ {@link java.util.concurrent.ThreadPoolExecutor}.
+ </dd>
+ <dt>
+ <b><a href="run-code.html">Running Code on a Thread Pool Thread</a></b>
+ </dt>
+ <dd>
+ Learn how to run a {@link java.lang.Runnable} on a thread from the thread pool.
+ </dd>
+ <dt>
+ <b><a href="communicate-ui.html">Communicating with the UI Thread</a></b>
+ </dt>
+ <dd>
+ Learn how to communicate from a thread in the thread pool to the UI thread.
+ </dd>
+</dl>
+
diff --git a/docs/html/training/multiple-threads/run-code.jd b/docs/html/training/multiple-threads/run-code.jd
new file mode 100644
index 000000000000..a828828345b0
--- /dev/null
+++ b/docs/html/training/multiple-threads/run-code.jd
@@ -0,0 +1,148 @@
+page.title=Running Code on a Thread Pool 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="#RunRunnable">Run a Runnable on a Thread in the Thread Pool</a></li>
+ <li><a href="#StopThread">Interrupt Running Code</a></li>
+</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>
+
+<p>
+ The previous lesson showed you how to define a class that manages thread pools and the tasks
+ that run on them. This lesson shows you how to run a task on a thread pool. To do this,
+ you add the task to the pool's work queue. When a thread becomes available, the
+ {@link java.util.concurrent.ThreadPoolExecutor} takes a task from the queue and runs it on the
+ thread.
+</p>
+<p>
+ This lesson also shows you how to stop a task that's running. You might want to do this if a
+ task starts, but then discovers that its work isn't necessary. Rather than wasting processor
+ time, you can cancel the thread the task is running on. For example, if you are downloading
+ images from the network and using a cache, you probably want to stop a task if it detects that
+ an image is already present in the cache. Depending on how you write your app, you may not be
+ able to detect this before you start the download.
+</p>
+<h2 id="RunRunnable">Run a Task on a Thread in the Thread Pool</h2>
+<p>
+ To start a task object on a thread in a particular thread pool, pass the
+ {@link java.lang.Runnable} to {@link java.util.concurrent.ThreadPoolExecutor#execute
+ ThreadPoolExecutor.execute()}. This call adds the task to the thread pool's work queue. When an
+ idle thread becomes available, the manager takes the task that has been waiting the longest and
+ runs it on the thread:
+</p>
+<pre>
+public class PhotoManager {
+ public void handleState(PhotoTask photoTask, int state) {
+ switch (state) {
+ // The task finished downloading the image
+ case DOWNLOAD_COMPLETE:
+ // Decodes the image
+ mDecodeThreadPool.execute(
+ photoTask.getPhotoDecodeRunnable());
+ ...
+ }
+ ...
+ }
+ ...
+}
+</pre>
+<p>
+ When {@link java.util.concurrent.ThreadPoolExecutor} starts a {@link java.lang.Runnable} on a
+ thread, it automatically calls the object's {@link java.lang.Runnable#run run()} method.
+</p>
+<h2 id="StopThread">Interrupt Running Code</h2>
+<p>
+ To stop a task, you need to interrupt the task's thread. To prepare to do this, you need to
+ store a handle to the task's thread when you create the task. For example:
+</p>
+<pre>
+class PhotoDecodeRunnable implements Runnable {
+ // Defines the code to run for this task
+ public void run() {
+ /*
+ * Stores the current Thread in the
+ * object that contains PhotoDecodeRunnable
+ */
+ mPhotoTask.setImageDecodeThread(Thread.currentThread());
+ ...
+ }
+ ...
+}
+</pre>
+<p>
+ To interrupt a thread, call {@link java.lang.Thread#interrupt Thread.interrupt()}. Notice that
+ {@link java.lang.Thread} objects are controlled by the system, which can modify them outside of
+ your app's process. For this reason, you need to lock access on a thread before you
+ interrupt it, by placing the access in a <code>synchronized</code> block. For example:
+</p>
+<pre>
+public class PhotoManager {
+ public static void cancelAll() {
+ /*
+ * Creates an array of Runnables that's the same size as the
+ * thread pool work queue
+ */
+ Runnable[] runnableArray = new Runnable[mDecodeWorkQueue.size()];
+ // Populates the array with the Runnables in the queue
+ mDecodeWorkQueue.toArray(runnableArray);
+ // Stores the array length in order to iterate over the array
+ int len = runnableArray.length;
+ /*
+ * Iterates over the array of Runnables and interrupts each one's Thread.
+ */
+ synchronized (sInstance) {
+ // Iterates over the array of tasks
+ for (int runnableIndex = 0; runnableIndex &lt; len; runnableIndex++) {
+ // Gets the current thread
+ Thread thread = runnableArray[taskArrayIndex].mThread;
+ // if the Thread exists, post an interrupt to it
+ if (null != thread) {
+ thread.interrupt();
+ }
+ }
+ }
+ }
+ ...
+}
+</pre>
+<p>
+ In most cases, {@link java.lang.Thread#interrupt Thread.interrupt()} stops the thread
+ immediately. However, it only stops threads that are waiting, and will not interrupt CPU or
+ network-intensive tasks. To avoid slowing down or locking up the system, you should test for
+ any pending interrupt requests before attempting an operation :
+</p>
+<pre>
+/*
+ * Before continuing, checks to see that the Thread hasn't
+ * been interrupted
+ */
+if (Thread.interrupted()) {
+ return;
+}
+...
+// Decodes a byte array into a Bitmap (CPU-intensive)
+BitmapFactory.decodeByteArray(
+ imageBuffer, 0, imageBuffer.length, bitmapOptions);
+...
+</pre>
diff --git a/docs/html/training/multiple-threads/threadsample.zip b/docs/html/training/multiple-threads/threadsample.zip
new file mode 100644
index 000000000000..bdc3ccfd7040
--- /dev/null
+++ b/docs/html/training/multiple-threads/threadsample.zip
Binary files differ
diff --git a/docs/html/training/training_toc.cs b/docs/html/training/training_toc.cs
index c4e0f844cf77..78b0dce03f72 100644
--- a/docs/html/training/training_toc.cs
+++ b/docs/html/training/training_toc.cs
@@ -925,6 +925,33 @@
</li>
</ul>
</li>
+ <li class="nav-section">
+ <div class="nav-section-header">
+ <a href="<?cs var:toroot ?>training/multiple-threads/index.html"
+ description=
+ "How to improve the performance and scalability of long-running operations by
+ dispatching work to multiple threads.">
+ Sending Operations to Multiple Threads</a>
+ </div>
+ <ul>
+ <li><a href="<?cs var:toroot ?>training/multiple-threads/define-runnable.html">
+ Specifying the Code to Run on a Thread
+ </a>
+ </li>
+ <li><a href="<?cs var:toroot ?>training/multiple-threads/create-threadpool.html">
+ Creating a Manager for Multiple Threads
+ </a>
+ </li>
+ <li><a href="<?cs var:toroot ?>training/multiple-threads/run-code.html">
+ Running Code on a Thread Pool Thread
+ </a>
+ </li>
+ <li><a href="<?cs var:toroot ?>training/multiple-threads/communicate-ui.html">
+ Communicating with the UI Thread
+ </a>
+ </li>
+ </ul>
+ </li>
<li>
<a href="<?cs var:toroot ?>training/articles/perf-anr.html"
diff --git a/services/java/com/android/server/InputMethodManagerService.java b/services/java/com/android/server/InputMethodManagerService.java
index 94e0bd460679..8eb61dd1ad0c 100644
--- a/services/java/com/android/server/InputMethodManagerService.java
+++ b/services/java/com/android/server/InputMethodManagerService.java
@@ -782,6 +782,9 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
if (!isSystemIme(imi)) {
return false;
}
+ if (imi.isAuxiliaryIme()) {
+ return false;
+ }
if (imi.getIsDefaultResourceId() != 0) {
try {
Resources res = context.createPackageContext(
@@ -805,6 +808,9 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
if (!isSystemIme(imi)) {
return false;
}
+ if (imi.isAuxiliaryIme()) {
+ return false;
+ }
return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
}
@@ -2856,6 +2862,9 @@ public class InputMethodManagerService extends IInputMethodManager.Stub
List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
.getEnabledInputMethodsAndSubtypeListLocked();
+ if (DEBUG) {
+ Slog.d(TAG, (enabled ? "Enable " : "Disable ") + id);
+ }
if (enabled) {
for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
if (pair.first.equals(id)) {