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.
线程池运行多个线程
最新推荐文章于 2024-08-15 10:46:07 发布