微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:取消对 usbDeviceConnection.requestWait();

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?