城市多线程

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.TestActivity6"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="城市1"
17         android:id="@+id/tv_3"/>
18 
19     <TextView
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="城市2"
23         android:id="@+id/tv_4"/>
24 
25     <Button
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="启动"
29         android:onClick="bt1_OnClick"/>
30 </LinearLayout>
  1 package com.hanqi.testapp2;
  2 
  3 import android.os.Message;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.os.Handler;
  7 import android.view.View;
  8 import android.widget.TextView;
  9 import android.widget.Toast;
 10 
 11 import java.util.Random;
 12 
 13 
 14 public class TestActivity6 extends AppCompatActivity {
 15 
 16     TextView tv_3;
 17     TextView tv_4;
 18 @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_test6);
 22 
 23         tv_3=(TextView)findViewById(R.id.tv_3);
 24         tv_4=(TextView)findViewById(R.id.tv_4);
 25 }
 26 String c1="北京";
 27     String c2="香港";
 28 
 29     public void bt1_OnClick(View v)
 30     {
 31         tv_3.setText("");
 32         tv_4.setText("");
 33 
 34 
 35         //创建子线程1
 36         new Thread(){
 37 
 38             @Override
 39             public void run() {
 40 
 41 
 42                 for (int i=0;i<10;i++)
 43                 {
 44 
 45                     runOnUiThread(new Runnable() {
 46                         @Override
 47                         public void run() {
 48 
 49 
 50                             tv_3.setText(tv_3.getText()+c1);
 51                 }
 52                     });
 53 
 54 
 55                     try {
 56                         //暂停时间随机
 57                         Thread.sleep(new Random().nextInt(2000));
 58                     }
 59                     catch (Exception e)
 60                     {
 61 
 62                     }
 63                 }
 64                 runOnUiThread(new Runnable() {
 65                     @Override
 66                     public void run() {
 67 
 68                         Toast.makeText(TestActivity6.this,c1+ "循环完成", Toast.LENGTH_SHORT).show();
 69 
 70                     }
 71                 });
 72 
 73 
 74             }
 75         }.start();
 76 
 77         //创建子线程2  实现接口
 78         new Thread(){
 79 
 80             @Override
 81             public void run() {
 82 
 83 
 84                 for (int i=0;i<10;i++)
 85                 {
 86 
 87                     runOnUiThread(new Runnable() {
 88                         @Override
 89                         public void run() {
 90 
 91 
 92                             tv_4.setText(tv_4.getText()+c2);
 93                         }
 94                     });
 95 
 96 
 97                     try {
 98                         //暂停时间随机
 99                         Thread.sleep(new Random().nextInt(2000));
100                     }
101                     catch (Exception e)
102                     {
103 
104                     }
105                 }
106                 runOnUiThread(new Runnable() {
107                     @Override
108                     public void run() {
109 
110                         Toast.makeText(TestActivity6.this,c2+ "循环完成", Toast.LENGTH_SHORT).show();
111 
112                     }
113                 });
114 
115 
116             }
117         }.start();
118     }
119 
120 }

  

原文地址:https://www.cnblogs.com/cycanfly/p/5499187.html