Android 中的 HandlerThread 安全
HandlerThread safety in Android
好吧,这是一个我认为不需要问的问题,但要么是工作时间太长,要么是什么原因,但我脑子里一片模糊,所以就开始吧。
使用 HandlerThread、Looper 和 Handler 创建 class:
public class MyClass {
//private volatile boolean mRunning = false;
private boolean mRunning = false;
private HandlerThread mHandlerThread = null;
private Handler mMessageHandler = null;
public static final int MESSAGE_START = 1;
public static final int MESSAGE_STOP = 2;
public MyClass() {
mHandlerThread = new HandlerThread("com.test.myclass");
mHandlerThread.start();
mMessageHandler = new MessageHandler(mHandlerThread.getLooper());
}
private class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
private void start() {
mRunning = true;
}
private void stop() {
mRunning = false;
}
@Override
public void handleMessage(final Message msg) {
try {
switch (msg.what) {
case MESSAGE_START:
start();
break;
case MESSAGE_STOP:
stop();
break;
default:
throw new RuntimeException("Invalid message: " + msg.what);
}
} catch (RuntimeException e) {
stop();
}
}
}
public void release() {
if (isRunning()) {
stop();
}
// PS: is this a good way to stop HandlerThead/Looper in API =< 17 ?
if (mHandlerThread != null) {
mHandlerThread.quit();
mHandlerThread = null;
}
}
// Should this be a synchronized method
public boolean isRunning() {
return mRunning;
/**
* Or should the variable be synchronized itself?
* synchronized(mRunning) { return mRunning; }
*/
// Or just use a semaphore?
}
public void start() {
mMessageHandler.sendEmptyMessage(MESSAGE_START);
}
public void stop() {
mMessageHandler.sendEmptyMessage(MESSAGE_STOP);
}
}
所以 mRunning
被两个线程(主线程和循环程序)访问。因此,访问应该是同步的。你会选择哪种方式?使变量可变(以便两个线程都具有最新的本地值),使 isRunning
方法同步?或者通过 synchronized(mRunning){...}
?
访问变量
最后,release
方法的实现,是否符合标准?或者您选择的任何其他方式?
如果对锁的争用很低,那么同步锁的成本与易失性访问的成本大致相同。如果conention很高,那么volatile版本可能会表现更好。
但是你不能这样做:
synchronized(mRunning) { ... } //ERROR!
mRunning 是一个布尔表达式。您不能同步布尔值,只能同步对象。
好吧,这是一个我认为不需要问的问题,但要么是工作时间太长,要么是什么原因,但我脑子里一片模糊,所以就开始吧。
使用 HandlerThread、Looper 和 Handler 创建 class:
public class MyClass {
//private volatile boolean mRunning = false;
private boolean mRunning = false;
private HandlerThread mHandlerThread = null;
private Handler mMessageHandler = null;
public static final int MESSAGE_START = 1;
public static final int MESSAGE_STOP = 2;
public MyClass() {
mHandlerThread = new HandlerThread("com.test.myclass");
mHandlerThread.start();
mMessageHandler = new MessageHandler(mHandlerThread.getLooper());
}
private class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
private void start() {
mRunning = true;
}
private void stop() {
mRunning = false;
}
@Override
public void handleMessage(final Message msg) {
try {
switch (msg.what) {
case MESSAGE_START:
start();
break;
case MESSAGE_STOP:
stop();
break;
default:
throw new RuntimeException("Invalid message: " + msg.what);
}
} catch (RuntimeException e) {
stop();
}
}
}
public void release() {
if (isRunning()) {
stop();
}
// PS: is this a good way to stop HandlerThead/Looper in API =< 17 ?
if (mHandlerThread != null) {
mHandlerThread.quit();
mHandlerThread = null;
}
}
// Should this be a synchronized method
public boolean isRunning() {
return mRunning;
/**
* Or should the variable be synchronized itself?
* synchronized(mRunning) { return mRunning; }
*/
// Or just use a semaphore?
}
public void start() {
mMessageHandler.sendEmptyMessage(MESSAGE_START);
}
public void stop() {
mMessageHandler.sendEmptyMessage(MESSAGE_STOP);
}
}
所以 mRunning
被两个线程(主线程和循环程序)访问。因此,访问应该是同步的。你会选择哪种方式?使变量可变(以便两个线程都具有最新的本地值),使 isRunning
方法同步?或者通过 synchronized(mRunning){...}
?
最后,release
方法的实现,是否符合标准?或者您选择的任何其他方式?
如果对锁的争用很低,那么同步锁的成本与易失性访问的成本大致相同。如果conention很高,那么volatile版本可能会表现更好。
但是你不能这样做:
synchronized(mRunning) { ... } //ERROR!
mRunning 是一个布尔表达式。您不能同步布尔值,只能同步对象。