如何解决Android:取消对 usbDeviceConnection.requestWait();
我设置了一个 Android 应用程序来执行一些到 USB 设备的中断传输。 一切正常,除了清理和关闭连接的代码。
我有一个线程设置为“轮询”来自设备的传入 URB(通过在循环中调用 usbDeviceConnection.requestWait()
,并在数据传入时给我一个回调)。我将线程设置为在 .countDown()
上调用 CountDownLatch
作为其 run()
方法中的最后一条语句。在我的关闭逻辑中,我在继续清理之前等待倒计时闩锁被释放。问题是,虽然清理和关闭逻辑大部分时间都可以正常工作,但有时它会挂起等待倒计时锁存器被释放。当应用程序处于这种状态时,我连接了调试器并暂停了 VM,发现 URB 轮询器线程卡在对 usbDeviceConnection.requestWait()
的调用上。我不知道为什么。如果所有未完成的URB都被取消并且线程被中断,为什么调用仍然卡住?
// Cancel the URBs if they are in flight (if not,I presume this does nothing)
fooURB.cancel();
barURB.cancel();
// Interrupt the URB poller thread,and wait for it to release the count down latch
incomingUrbPoller.interrupt();
acquireUninterruptibly(pollerStopSynchronizer);
// Now that all URBs are cancelled and the URB poller is dead,it should be safe to close the URBs
fooURB.close();
barURB.close();
// Finally,release the interface and close the connection
usbDeviceConnection.releaseInterface(usbInterface);
usbDeviceConnection.close();
usbDeviceConnection = null;
URB 轮询线程:
private class IncomingURB_Poller extends Thread
{
@Override
public void run()
{
Thread.currentThread().setName("IncomingURB_Poller");
while(!Thread.currentThread().isInterrupted())
{
UsbRequest incomingURB = usbDeviceConnection.requestWait();
if(incomingURB != null)
{
handleIncomingURB(incomingURB);
}
}
pollerStopSynchronizer.countDown();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。