如何使用Handler类非并发/并行使用postDelayed。或者在一个线程中?

时间:2014-07-09 13:38:23

标签: android concurrency

这很奇怪。为什么我postDelay 1000ms,比如1000次,他们似乎同时运行所有?我希望它每100毫秒运行一次。一切都马上就出来了!

或者我是否需要配置一些内容以使其并发。我使用的是Android 4.0。

这是我的代码:

    public void startAniHandler() {

    Runnable runnable = new Runnable() {
        @Override 
        public void run() {
            Log.v("running", "REad image and display handler");

        }
    };

    Handler handler = new Handler(new android.os.Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Log.v("stopping", "handler");
            return false;
        }
    });

    for(int i=0;i<100;i++) {
        handler.postDelayed(runnable, 1000);
    }


}

我的输出是:

  

07-09 09:26:20.630 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.630 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.640 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.640 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.640 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.640 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.640 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.660 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.660 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.670 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.670 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.670 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.670 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.690 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.690 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.690 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.700 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.700 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.730 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.730 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.730 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.730 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.740 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.790 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.790 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.790 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.800 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.810 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.820 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.860 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.860 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.860 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.860 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.860 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.870 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.890 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.910 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.910 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.910 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.920 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.930 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.970 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序   07-09 09:26:20.980 906-906 / com.example.myappLoadingIndicatorWithAnimation V / running:READ图像和显示处理程序

1 个答案:

答案 0 :(得分:0)

Luksprog所说的工作:

hat for循环将以当前时间为起点发布Runnable,以便所有人同时完​​成,近似。你可能想要这样的东西:handler.postDelayed(runnable,1000 + 1000 * i);