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.JdbcOdbc...