package com.example.wang.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class ZuoyeActivity extends AppCompatActivity {
    Button bt_1;
    int j=1;
    int k=1;
    TextView tv_1;
    TextView tv_2;
    @Override
    protected void onCreate(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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                j=k=1;
                TestThread t1 = new TestThread();
                t1.start();
                Toast.makeText(ZuoyeActivity.this, "t1运行", Toast.LENGTH_SHORT).show();
                Log.e("TAG","t1运行");
                TestThread1 t2 = new TestThread1();
                t2.start();
                Toast.makeText(ZuoyeActivity.this, "t2运行", Toast.LENGTH_SHORT).show();
                Log.e("TAG", "t2运行");
            }
        });
    }
class TestThread extends Thread
    {
        Random random=new Random();
        public void run()
        {
            for (int i=0;i<10;i++)
            {
                On();
            }
        }
    }
    public  void On()
    {
        Random random=new Random();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_1.setText("上海"+j);
                  j++;
            }
        });
        try
        {
            int t=random.nextInt(1000);
            Thread.sleep(t);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_1.setText("");
            }
        });
        try
        {
            int t=random.nextInt(1000);
            Thread.sleep(t);
            Log.e("TAG", "t=" +t );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
class TestThread1 extends Thread
    {
        Random random=new Random();
        public void run()
        {
            for (int i=0;i<10;i++)
            {
                On1();
            }
        }
    }
    public  void On1()
    {
        Random random=new Random();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_2.setText("北京"+k);
                k++;
            }
        });
        try
        {
            int t=random.nextInt(1000);
            Thread.sleep(t);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_2.setText("");
            }
        });
        try
        {
            int t=random.nextInt(1000);
            Thread.sleep(t);
            Log.e("TAG", "t=" +t );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}