Smartcardio死锁

时间:2014-04-02 08:18:52

标签: java api smartcard

分析我的应用程序(使用你的套件)我发现java api陷入僵局(冻结7小时23分14秒)。

这是剖析器打印的内容:

Frozen threads found (potential deadlock)

It seems that the following threads have not changed their stack for more than 10 seconds.
These threads are possibly (but not necessarily!) in a deadlock or hung.

Thread-13 <--- Frozen for at least 7h 23m 14 sec
sun.security.smartcardio.PCSC.SCardListReaders(long)
sun.security.smartcardio.PCSCTerminals.list(CardTerminals$State)
javax.smartcardio.CardTerminals.list()
xxx.xxxxxx.cardreader.CardReader.run()

这就是我如何使用api(我只提取有用的代码)

 public void run() {
    protocolArr = {"T=0", "T=1"}
    while(true) {
        try {
            Thread.sleep(1000);             

            TerminalFactory terminalFactory = TerminalFactory.getDefault();
            List<CardTerminal> terminalList = terminalFactory.terminals().list();   
            for(CardTerminal terminal : terminalList) {
                Card card = null;                   
                // establish a connection with the card
                for(String protocol : protocolArr) {
                    try {
                        card = terminal.connect(protocol);
                        break;
                    } catch(Exception e) {
                    }
                }
                if(card == null)
                    continue;                   
                // The card is valorized                    
                CardChannel channel = card.getBasicChannel();                   
                // SN request
                byte[] byteArr = new byte[5];                   
                // FFh FCh 01h 00h 00h 
                byteArr[0] = (byte) 0xFF;
                byteArr[1] = (byte) 0xFC;
                byteArr[2] = 0x00; // 00h : ATQA + SN + SAK
                byteArr[3] = 0x00;
                byteArr[4] = 0x00;                  
                ResponseAPDU r = channel.transmit(new CommandAPDU(byteArr));                    
                // 44h 03h 04h 26h 47h 09h 48h E8h 10h 20h 90h 00h (example) 
                //      ATQA = 44h 03h 04h 
                //      SN = 26h 47h 09h 48h E8h 10h (7 bytes) 
                //      SAK = 20h                   
                byte[] response = r.getBytes();                 
               // disconnect
                card.disconnect(false);
            }
        } catch(Exception e) {
        }
    }
}

有人遇到过这个问题并且有修复/解决方法吗?

1 个答案:

答案 0 :(得分:0)

最好使用waitForChange方法而不是轮询卡终端。

请注意,CardTerminals会返回terminalFactory.terminals(),因此请先致电list()