示例代码:
final LinkedBlockingQueue<Boolean> waitConnected = new LinkedBlockingQueue<Boolean>(1);
boolean result = transportFactory.connect(context, new BTChipTransportFactoryCallback() {
@Override
public void onConnected(boolean success) {
try {
waitConnected.put(success);
} catch (InterruptedException ignore) {
}
}
});
if (result) {
try {
initialized = waitConnected.poll(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
if (initialized) {
initialized = proxy.init();
}
}
使用上面示例代码的方式,不用额外的同步代码或者线程,不用担心异步的方法返回太快导致Object.wait()方法获取不到notify通知,而且代码精简,是个好办法。
文档信息
- 本文作者:itlgl
- 本文链接:https://itlgl.com/note/2018/03/19/issues-6/
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)