Created
December 12, 2018 16:05
-
-
Save cutiko/b88cd91777855ab961c023208aff0cf1 to your computer and use it in GitHub Desktop.
ThreadPoolExecutor java example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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