线程池运行多个线程

 final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
                    for (final MSContact contact : contactList) {
                        fixedThreadPool.execute(new Runnable() {
                            @Override
                            public void run() {
                                AddContactInDeviceUtils.addContact(context, contact.getName(), contact.getFirstMobile(), contact.getSecondMobile());
                            }
                        });
                    }
                    fixedThreadPool.shutdown();

                    final Handler handler = new Handler();
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            if (fixedThreadPool.isTerminated()) {
                                dialog.dismiss();
                                ToastUtils.showToast(context, "已导入" + contactList.size() + "个联系人");
                                Intent intent = new Intent();
                                intent.setAction("com.newingscom.iwcontact.main.update");
                                context.sendBroadcast(intent);
                                context.finish();
                            } else {
                                handler.postDelayed(this, 100);
                            }
                        }
                    };
                    handler.postDelayed(runnable, 100);//每两秒执行一次runnable.

你可能感兴趣的:(Android)