Multi-threaded programming-two cities to travel

Source: Internet
Author: User

 Packagecom.example.wang.myapplication;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportJava.util.Random; Public classZuoyeactivityextendsappcompatactivity {Button bt_1; intJ=1; intK=1;    TextView tv_1;    TextView tv_2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_zuoye); Bt_1=(Button) Findviewbyid (r.id.bt_1); Tv_1=(TextView) Findviewbyid (r.id.tv_1); Tv_2=(TextView) Findviewbyid (r.id.tv_2); Bt_1.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {J=k=1; Testthread T1=NewTestthread ();                T1.start (); Toast.maketext (zuoyeactivity. This, "T1 Run", Toast.length_short). Show (); LOG.E ("TAG", "T1 Run"); TestThread1 T2=NewTestThread1 ();                T2.start (); Toast.maketext (zuoyeactivity. This, "T2 Run", Toast.length_short). Show (); LOG.E ("TAG", "T2 Run");    }        }); }classTestthreadextendsThread {random random=NewRandom ();  Public voidrun () { for(inti=0;i<10;i++) {on (); }        }    }     Public  voidOn () {random random=NewRandom (); Runonuithread (NewRunnable () {@Override Public voidrun () {Tv_1.settext ("Shanghai" +j); J++;        }        }); Try        {            intT=random.nextint (1000);        Thread.Sleep (t); }        Catch(Exception e) {e.printstacktrace (); } runonuithread (NewRunnable () {@Override Public voidrun () {Tv_1.settext ("");        }        }); Try        {            intT=random.nextint (1000);            Thread.Sleep (t); LOG.E ("TAG", "t=" +t); }        Catch(Exception e) {e.printstacktrace (); }    }classTestThread1extendsThread {random random=NewRandom ();  Public voidrun () { for(inti=0;i<10;i++) {On1 (); }        }    }     Public  voidOn1 () {random random=NewRandom (); Runonuithread (NewRunnable () {@Override Public voidrun () {Tv_2.settext ("Beijing" +k); K++;        }        }); Try        {            intT=random.nextint (1000);        Thread.Sleep (t); }        Catch(Exception e) {e.printstacktrace (); } runonuithread (NewRunnable () {@Override Public voidrun () {Tv_2.settext ("");        }        }); Try        {            intT=random.nextint (1000);            Thread.Sleep (t); LOG.E ("TAG", "t=" +t); }        Catch(Exception e) {e.printstacktrace (); }    }}
Java
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.wang.myapplication.ZuoyeActivity"android:orientation= "vertical">    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#ccc"android:textsize= "20SP"android:gravity= "Center"Android:id= "@+id/tv_1"/>    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "40DP"Android:background= "#cba"android:textsize= "20SP"android:gravity= "Center"Android:layout_margintop= "5DP"Android:id= "@+id/tv_2"/>    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Where to Travel"Android:id= "@+id/bt_1"Android:onclick= "Bt_1"/></LinearLayout>
XML

Multi-threaded programming-two cities to travel

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.