Android 子线程测试

    private volatile boolean mStopped = false;
    private int i;
    TextView tv1;
    TextView tv2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);

        Button button4 = (Button) findViewById(R.id.button4);    //获取“开始”按钮
        Button button5 = (Button) findViewById(R.id.button5);    //获取“停止”按钮

        //开始按钮
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( mStopped = true) {
                    mStopped = false;
                    Log.i("主线程ID", String.valueOf(Thread.currentThread().getId()));
                    Log.i("主线程var", String.valueOf(Thread.currentThread().getName()));

                    fgetsum(String.valueOf("mytv1"), 1);
                    fgetsum(String.valueOf("mytv2"), 2);
                }
            }
        });


        //结束线程按钮
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    mStopped = true;
            }
        });




    }






    //定义Handler 消息处理类,Handler 允许发送和处理 Message 或 Rannable 对像到其所在线程的 MessageQueue中
    //Handler接收Message通知,并刷新tv1
    //一般在这里刷新UI
    final Handler handler2 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0x101:
                    tv1.setText(String.valueOf(msg.obj.toString()));
                    break;
                case 0x102:
                    tv2.setText(String.valueOf(msg.obj.toString()));
                    break;
                default:
                    tv1.setText(String.valueOf("未知msg.what"));
                    break;
            }

        }
    };



    //定义Runnable接口
    //在子线程中发送Message给Handler
    //Runnable不会在UI运行,所以在这里不能直接修改UI对象属性
    private void fgetsum(final String strname, final int tvid) {
        Thread thread = new Thread() {
            @Override
            public void run() {


                while (mStopped == false) {
                    i++;
                    try {
                        Thread.sleep(1000);
                        Log.i("子线程ID" + strname, String.valueOf(Thread.currentThread().getId()));
                        Log.i("子线程名" + strname, String.valueOf(Thread.currentThread().getName()));

                        Message message = handler2.obtainMessage();
                        message.arg1 = tvid;
                       //如果tvid=1传入时间,否则传入i
                        if(tvid==1){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.getDefault());
                            message.what=0x101;
                            message.obj = String.valueOf(sdf.format(new Date()));
                        }else{
                            message.what=0x102;
                            message.obj =i;
                        }


                        handler2.sendMessage(message);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

        }
        };
        thread.start();
        thread = null;
    }

 

Android 跟winform一样,,主线程上不能直接更新UI上的控件,否则界面会假死(比如进度条、下载等任务)

在ASP.NET下有BackgroundWorker,而Android一般用Handler+Thread+Message来实现,对于用惯了.net的人说,看JAVA程序会很不习惯,很多功能实现起来比.NET麻烦多了。。。。。

 

另外查了一下JAVA资料,如果要创建N个线程,建议用线程序池(ExecutorService)更好.

 

你可能感兴趣的:(android)