Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created December 12, 2018 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cutiko/b88cd91777855ab961c023208aff0cf1 to your computer and use it in GitHub Desktop.
Save cutiko/b88cd91777855ab961c023208aff0cf1 to your computer and use it in GitHub Desktop.
ThreadPoolExecutor java example
import android.os.Process;
import android.util.Log;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by cutiko on 30-11-17.
*/
public class GeoExecutor extends ThreadPoolExecutor {
private static final GeoExecutor ourInstance = new GeoExecutor();
public static GeoExecutor getInstance() {
return ourInstance;
}
private GeoExecutor() {
super(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors()*2, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(), new BackgroundThreadFactory());
}
private static class BackgroundThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("CustomThread" + "GeoExecutor");
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("THREAD_ERROR", thread.getName() + " encountered an error: " + ex.getMessage());
}
});
return thread;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment