城市线程练习题后续

随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

 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.example.wang.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 
31 
32 </LinearLayout>
.xml
  1 package com.example.wang.testapp2;
  2 
  3 import android.os.Handler;
  4 import android.os.Message;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.view.View;
  8 import android.widget.TextView;
  9 import android.widget.Toast;
 10 import java.util.Random;
 11 
 12 
 13 public class TestActivity6 extends AppCompatActivity {
 14 
 15     TextView tv_3,tv_4;
 16 
 17 
 18 
 19     @Override
 20     protected void onCreate(Bundle savedInstanceState) {
 21         super.onCreate(savedInstanceState);
 22         setContentView(R.layout.activity_test6);
 23 
 24         tv_3=(TextView)findViewById(R.id.tv_3);
 25         tv_4=(TextView)findViewById(R.id.tv_4);
 26     }
 27 
 28 
 29 
 30     String c1="北京";
 31     String c2="香港";
 32 
 33 //    public void bt1_OnClick(View v)
 34 //    {
 35 //        //创建子线程1
 36 //        new Thread(){
 37 //            @Override
 38 //            public void run() {
 39 //
 40 //                for (int i=0;i<20;i++)
 41 //                {
 42 //                    if (i%2==0)
 43 //                    {
 44 //                        c1="";
 45 //                    }
 46 //                    else
 47 //                    {
 48 //                        c1="北京";
 49 //                    }
 50 //
 51 //                    runOnUiThread(new Runnable() {
 52 //                        @Override
 53 //                        public void run() {
 54 //
 55 //                            tv_3.setText(c1);
 56 //
 57 //                        }
 58 //                    });
 59 //
 60 //                    try {
 61 //
 62 //                        //暂停时间随机
 63 //                        Thread.sleep(new Random().nextInt(2000));
 64 //
 65 //                    }catch (Exception e) {
 66 //
 67 //                    }
 68 //                }
 69 //
 70 //                runOnUiThread(new Runnable() {
 71 //                    @Override
 72 //                    public void run() {
 73 //
 74 //                        Toast.makeText(TestActivity6.this, c1 + "循环完成", Toast.LENGTH_SHORT).show();
 75 //
 76 //                    }
 77 //                });
 78 //            }
 79 //        }.start();
 80 //
 81 //
 82 //        //创建子线程2
 83 //        new Thread(){
 84 //            @Override
 85 //            public void run() {
 86 //
 87 //                for (int i=0;i<20;i++)
 88 //                {
 89 //                    if (i%2==0)
 90 //                    {
 91 //                        c2="";
 92 //                    }
 93 //                    else
 94 //                    {
 95 //                        c2="香港";
 96 //                    }
 97 //
 98 //                    runOnUiThread(new Runnable() {
 99 //                        @Override
100 //                        public void run() {
101 //
102 //                            tv_4.setText(c2);
103 //
104 //                        }
105 //                    });
106 //
107 //                    try {
108 //
109 //                        //暂停时间随机
110 //                        Thread.sleep(new Random().nextInt(2000));
111 //
112 //                    }catch (Exception e) {
113 //
114 //                    }
115 //                }
116 //
117 //                runOnUiThread(new Runnable() {
118 //                    @Override
119 //                    public void run() {
120 //
121 //                        Toast.makeText(TestActivity6.this, c2 + "循环完成", Toast.LENGTH_SHORT).show();
122 //
123 //                    }
124 //                });
125 //            }
126 //        }.start();
127 //    }
128 
129     public void bt1_OnClick(View v)
130     {
131         tv_3.setText("");
132         tv_4.setText("");
133 
134 
135         //创建子线程1
136         new Thread(){
137             @Override
138             public void run() {
139 
140                 for (int i=0;i<10;i++)
141                 {
142 
143                     runOnUiThread(new Runnable() {
144                         @Override
145                         public void run() {
146 
147                             tv_3.setText(tv_3.getText()+c1);
148 
149                         }
150                     });
151 
152                     try {
153 
154                         //暂停时间随机
155                         Thread.sleep(new Random().nextInt(2000));
156 
157                     }catch (Exception e) {
158 
159                     }
160                 }
161 
162                 runOnUiThread(new Runnable() {
163                     @Override
164                     public void run() {
165 
166                         Toast.makeText(TestActivity6.this, c1 + "循环完成", Toast.LENGTH_SHORT).show();
167 
168                     }
169                 });
170             }
171         }.start();
172 
173 
174         //创建子线程2
175         new Thread(){
176             @Override
177             public void run() {
178 
179                 for (int i=0;i<10;i++)
180                 {
181 
182                     runOnUiThread(new Runnable() {
183                         @Override
184                         public void run() {
185 
186                             tv_4.setText(tv_4.getText()+c2);
187 
188                         }
189                     });
190 
191                     try {
192 
193                         //暂停时间随机
194                         Thread.sleep(new Random().nextInt(2000));
195 
196                     }catch (Exception e) {
197 
198                     }
199                 }
200 
201                 runOnUiThread(new Runnable() {
202                     @Override
203                     public void run() {
204 
205                         Toast.makeText(TestActivity6.this, c2 + "循环完成", Toast.LENGTH_SHORT).show();
206 
207                     }
208                 });
209             }
210         }.start();
211     }
212 }
.java

原文地址:https://www.cnblogs.com/arxk/p/5498431.html