android--Multithreaded Programming Exercises

Source: Internet
Author: User

Choose from two cities as a pre-selected destination. The implementation of two separate threads display 10 city names, each time after each display sleep a random time (within 1000ms), which first displayed, decided to go to which city. Implemented using the Runnable interface and the thread class, respectively.

Layout file:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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"Ten Tools:context= "Com.hanqi.testapp2.PractiseActivity4" One android:orientation= "vertical"> A  -     <TextView -         Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:id= "@+id/tvw1"/> -     <TextView -         Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" - Android:id= "@+id/tvw2"/> +  A     <Button at         Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:id= "@+id/btt1" - Android:text= "Random Selection" - Android:onclick= "Btn_onclick"/> - </LinearLayout>

Java class:

1  Packagecom.hanqi.testapp2;2 3 ImportAndroid.os.Bundle;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.view.View;6 ImportAndroid.widget.Button;7 ImportAndroid.widget.TextView;8 ImportAndroid.widget.Toast;9 Ten  Public classPractiseActivity4extendsappcompatactivity { One  A TextView tvw1; - Button btt1; - TextView tvw2; the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (R.LAYOUT.ACTIVITY_PRACTISE4); +TVW1 =(TextView) Findviewbyid (R.ID.TVW1); -BTT1 =(Button) Findviewbyid (R.ID.BTT1); +TVW2 =(TextView) Findviewbyid (R.ID.TVW2); A     } atString c1 = "Beijing"; -String C2 = "Shanghai"; -      Public voidBtn_onclick (View v) -     { -         //Creating a child thread 1 -        NewThread () { in @Override -             Public voidrun () { to                 for(inti=0;i<20;i++) +                { -                    if(i%2==0) the                    { *C1= ""; $                    }Panax Notoginseng                    Else -                    { thec1= "Beijing"; +                    } ARunonuithread (NewRunnable () { the @Override +                         Public voidrun () { - Tvw1.settext (C1); $                        } $                    }); -                    //Pause -                    Try { theThread.Sleep ((int) (Math.random () *1000)); -                    }Wuyi                   Catch(Exception e) the                   { -  Wu                   } -                } AboutRunonuithread (NewRunnable () { $ @Override -                     Public voidrun () { -Toast.maketext (PractiseActivity4. This, C1 + "loop complete", Toast.length_short). Show (); -                    } A                }); +            } the }.start (); -         //Creating a child thread 2 $         NewThread () { the @Override the              Public voidrun () { the                  for(inti=0;i<20;i++) the                 { -                     if(i%2==0) in                     { theC2= ""; the                     } About                     Else the                     { theC2= "Shanghai"; the                     } +Runonuithread (NewRunnable () { - @Override the                          Public voidrun () {Bayi Tvw2.settext (C2); the                         } the                     }); -                     //Pause -                     Try { theThread.Sleep ((int) (Math.random () *1000)); the                     } the                     Catch(Exception e) the                     { -  the                     } the                 } theRunonuithread (NewRunnable () {94 @Override the                      Public voidrun () { theToast.maketext (PractiseActivity4. This, C2 + "loop complete", Toast.length_short). Show (); the                     }98                 }); About             } - }.start ();101     }102 103}

Choose from two cities as a pre-selected destination. The implementation of two separate threads display 10 city names, each time after each display sleep a random time (within 1000ms), which first displayed, decided to go to which city. Implemented using the Runnable interface and the thread class, respectively.

android--Multithreaded Programming Exercises

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.