<span id="Label3"></p><p><p><strong>What is a process</strong></p></p><p><p><strong> </strong>A process is a standalone (self contained) running environment, which can be thought of as a program or an Application.</p></p><p><p><strong>What is a thread</strong></p></p><p><p><strong> </strong>While threads are a task that executes in a process, the Java Runtime Environment is a single process that contains different tiredness and programs. Threads can be called lightweight processes. Threads require fewer resources to create and reside in a process, and can share resources in a process.</p></p><p><p><strong>Android Threads</strong></p></p><p><p>Android threads, in fact, are not inherently different from the multithreaded programming of Java. When we need to perform some time-consuming operations, such as the launch of a network request, for other reasons, such as speed, the server may not respond to our request immediately, if you do not put such operations in a sub-thread, it will cause the main thread is blocked, affecting the User's normal use of the Software.</p></p><p><p><strong>Basic usage of threads</strong></p></p><p><p><strong> </strong>Android multithreaded programming is no more special than Java multithreading, basically using the same syntax. There are two ways to create a thread:</p></p><p><p>One is to create a new class that inherits from thread, then rewrite the parent Class's run () method and edit the time-consuming logic in it, such as:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">thread{</span><span style="color: #008080;">2</span><span style="color: #000000;"> @Override</span><span style="color: #008080;">3</span>public <span style="color: #0000ff;"></span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> run () { </span> <span style="color: #008080;">4</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Specific processing of logic</span> <span style="color: #008080;">5</span><span style="color: #000000;"> }</span><span style="color: #008080;">6</span> }</pre></pre><p><p>The method of starting this thread is also very simple, just need to new out of the Mythread instance, and then call its start method, so that the code in the Run () method will run in the child thread, such as</p></p><pre><pre><span style="color: #0000ff;">New</span> MyThread (). Start ();</pre></pre><p><p>however, the use of inherited coupling is relatively high, more time using the implementation of the Runnable interface is a good choice, such as:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">Implements</span> <span style="color: #000000;">runnable{</span><span style="color: #008080;">2</span><span style="color: #000000;"> @Override</span><span style="color: #008080;">3</span>public <span style="color: #0000ff;"></span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> run () { </span> <span style="color: #008080;">4</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Specific processing of logic</span> <span style="color: #008080;">5</span><span style="color: #000000;"> }</span><span style="color: #008080;">6</span> }</pre></pre><p><p>If you use this notation, the method of starting the thread also needs to be changed, such as:</p></p><pre><pre><span style="color: #0000ff;">New</span> <span style="color: #000000;">MyThread ();</span> <span style="color: #0000ff;">New</span> Thread (myThread). Start ();</pre></pre><p><p>As you can see, the Thread's constructor receives a runnable parameter, and the new Mythread is an object that implements the Runnable interface, so it can be passed directly into the Thread's Constructor. The start () method of the thread is then called, and the code in the Run () method runs in the child Thread.</p></p><p><p><strong>Multi-threading in Android</strong></p></p><p><p><strong> </strong>Like many other GUI libraries, Android's UI is also thread Insecure. That is, if you want to update the UI elements in your app, you must do so in the main thread, or an exception will Occur.</p></p><p><p>For example, Create a new androidthreadtest project, and then modify the code in the activity</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Relativelayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Xmlns:tools</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/tools"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:paddingbottom</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@dimen/activity_vertical_margin"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:paddingleft</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@dimen/activity_horizontal_margin"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:paddingright</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@dimen/activity_horizontal_margin"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:paddingtop</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@dimen/activity_vertical_margin"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Tools:context</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Mainactivity "</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/change_text"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "change text"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">TextView</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/text"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "Hello world"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:textsize</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "20sp"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Relativelayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p></p></p><p><p>Two controls are defined in the layout file, TextView is used to display a Hello World string in the middle of the screen, and the button is used to change what is displayed in the TEXTVIEW. We would like to change the string displayed in TextView to Meey for you after clicking the Button.</p></p><p><p>next, Modify the code in Mainactivity:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Activity<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">onclicklistener{</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button changetext;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">TextView text;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate);</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">Setcontentview (r.layout.activity_main);</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Text =<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.text);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>Changetext =<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.change_text);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>Changetext.setonclicklistener (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Oncreateoptionsmenu (menu Menu) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Inflate the menu; this adds items to the action bar if it is Present.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">getmenuinflater (). Inflate (r.menu.main, menu);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.change_text:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Thread (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Run () {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Text.settext ("nice-meet you"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;">}). start ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span>}</pre><p><p>As you can see, a child thread is opened in the Click event of the change text button, and then the TextView settext () method is called in the child thread to modify the string, the logic of the code is very simple, but it is to press the button and then update the text, and then run the program, You can see that the program will crash as Expected.</p></p><p><p>The program crashes because the UI is being updated in a child thread, so you can see that Android does not allow UI operations on child THREADS. But there are times when we have to perform some time-consuming tasks in the subprocess and then update the corresponding UI controls based on the results of the task, and for that, you need to use an asynchronous message processing mechanism provided by ANDROID.</p></p><p><p>Android Multithreaded Programming (a)--multithreading Basics</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service