Files
tlskit/net/Threads.java
T
2026-10-18 19:03:33 +02:00

44 lines
1.5 KiB
Java

package com.minamp.net;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Background threads with big stacks. Android 2.x gives new threads ~16 KB of stack, and the
* bundled TLS handshake + certificate parsing needs far more; overflowing it kills the app.
* 256 KB is the most Dalvik allows.
*/
public final class Threads {
public static final long STACK = 256 * 1024;
private Threads() {}
public static Thread create(Runnable r, String name) {
Thread t = new Thread(null, r, name, STACK);
t.setDaemon(true);
return t;
}
public static void start(Runnable r, String name) {
create(r, name).start();
}
public static ExecutorService pool(final String name, int n) {
ThreadFactory f = new ThreadFactory() {
private int count;
public synchronized Thread newThread(Runnable r) { return create(r, name + "-" + (++count)); }
};
return n == 1 ? Executors.newSingleThreadExecutor(f) : Executors.newFixedThreadPool(n, f);
}
/** Readable one-liner for any failure, including Errors such as StackOverflowError. */
public static String describe(Throwable t) {
String m = t.getMessage();
String cls = t.getClass().getSimpleName();
if (m == null || m.length() == 0) return cls;
if (t instanceof java.io.IOException) return m;
return cls + ": " + m;
}
}