最近上网看了别人分享的工作线程code,我也臭不要脸的转发一下
private static final class Worker implements Runnable {
private static final Object mLock = new Object();
private Looper mLooper;
public Worker(String name) {
final Thread thread = new Thread(null, this, name);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
synchronized (mLock) {
while (mLooper == null) {
try {
mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void run() {
synchronized (mLock) {
// 该方法只能执行一次,一个Thread只能关联一个Looper
Looper.prepare();
mLooper = Looper.myLooper();
mLock.notifyAll();
}
Looper.loop();
}
public Looper getLooper() {
return mLooper;
}
public void quit() {
mLooper.quit();
}
}