I'm trying a simple test.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int N = 20;
private final List<ImageView> images = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout root = ((GridLayout) findViewById(R.id.root));
for (int i = 0; i < N; i++) {
ImageView image = new ImageView(this);
images.add(image);
root.addView(image, 100, 100);
}
findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
load();
}
});
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build()
);
load();
}
private void load() {
for (int i = 0; i < images.size(); i++)
Glide.with(this).load("https://dummyimage.com/100x100/000/fff&text=" + (i + 1)).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).priority(Priority.IMMEDIATE).into(images.get(i));
}
}
MyGlideModule.java
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(logging);
client.addNetworkInterceptor(new StethoInterceptor());
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client.build());
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
The problem is that images are loaded one by one, though I expect the will be loaded simultaneously.
It well shown on the Chrome network trace (thanks to Stetho)
Is it possible make glide to start loading all the images at once?
The solution is manually set pool executors for Glide (and optionally for OkHttp):
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setResizeService(new FifoPriorityThreadPoolExecutor(100));
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectionPool(new ConnectionPool(100, 3, TimeUnit.SECONDS));
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(builder.build());
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
By default Glide uses thread pool with size, which equals count of CPU cores. So if it detects just 1 core, it will do only one load a time.
You can define as many threads in pool, as you want (here 100, for example).
In turn, OkHttp also has default limitation on connections pool, and it is not so mush, as you may expect (I found just 4 on emulator). You can increase this limit by passing connection pool object with custom parameters to OkHttp builder.