本文介绍了由另一个 Thread Java smartcardio 建立的独占访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我很感激本网站上的许多有用答案,但我发现需要发布我的第一个问题(如果您发现我的帖子中有任何需要改进的地方,请告诉我).

I have appreciated many helpful answers on this site but I have found a need to post my first question (if you notice anything to be improved in my post let me know).

我有一个中等大小的带有 GUI 的 Java 程序,它充当中间人"和控制器.在信息流的一端,它通过 HTTP 服务器发送和接收数据.另一方面,它与 API 交互,其中数据最终与智能卡交换.中间"是 GUI、日志记录和其他一些功能.

I have a modest sized Java program with GUI that is acting as a "middleman" and controller. On one end of the information flow it sends and receives data via an HTTP server. On the other it is interacting with an API where data is ultimately exchanging with a SmartCard. In the "middle" is the GUI, logging, and some other features.

还有一个功能(通过 GUI 启动)偶尔加载智能卡的更新.否则,与智能卡的交换将通过 HTTP 发起.

There is also a feature (initiated via the GUI) to occasionally load an update to the SmartCard. Otherwise exchanges with the SmartCard are initiated over HTTP.

问题是在这两种模式之间切换时(将 http 与智能卡通信,然后切换到加载更新,反之亦然).

The problem is when switching between these 2 modes (communicating http to smartcard and then switching to loading the update or vice versa).

当我这样做时,我得出结论,我遇到了

When I do that I have concluded I run into the problem of

CardException: 由另一个线程建立的独占访问

sun.security.smartcardio

搜索网络显示异常似乎来自的代码是

Searching the web shows the code that exception appears to come from is

void checkExclusive() throws CardException {
        Thread t = exclusiveThread;
          if (t == null) {
                return;
            }
             if (t != Thread.currentThread()) {
            throw new CardException("Exclusive access established by another Thread");
        }
    }

我的第一个想法是我需要在每次需要时实例化 SmartCard API(然后将其设置回 null),而不是像我最初那样在整个程序中实例化一次.

My first thought was I needed to instantiate the SmartCard API each time I need it (and then set it back to null) instead of once for the entire program like I had initially.

这适用于通过 http 的交换,我认为这是因为对 handle() 方法的每个请求都是一个新线程.

This works for the exchanges over http and I figure it is because each request to the handle() method is a new thread.

在 GUI 中,更新由一个 ActionEvent 启动,它创建了一个 CardUpdate 的实例.然后在该类中获取 SmartCard API 的实例.

In the GUI the update is initiated by an ActionEvent which makes an instance of a CardUpdate. Inside that class then gets an instance of the SmartCard API.

我想如果在 actionPerformed 触发时我将操作放在不同的临时线程上,我的运气可能会更好.到目前为止,没有.

I thought maybe I'd have better luck if when actionPerformed triggered I put the actions on a different, temporary, thread. So far, no.

我得到的最接近的是使用类似的东西:

The closest I got was using something like:

SwingWorker worker = new SwingWorker<ImageIcon[], Void>() {

Sun 网站

使用它我可以进行更新,然后返回到 http 交换,但我无法进行另一次更新(根据 SwingWorker 的一次性使用规定)

Using that I could do an update and then go back to http exchanges but I couldn't do another update (per the one time use stipulation of SwingWorker)

然后我尝试根据需要制作多个 SwingWorker 做类似

I then tried making multiple SwingWorker as needed doing something like

private class GUICardUpdate extends SwingWorker<Integer, Void > {

但后来我又回到了原来的问题.我还尝试以这种方式在 GUI 类之外做一个简单的附加线程:

but then I was back to my original problem. I have also tried to just do a simple additional thread off the GUI class in this fashion:

public class GUI extends javax.swing.JFrame implements ActionListener, Runnable

但这也不例外.

也许我对线程的理解不够好,或者我忽略了一些简单的东西.有人有什么想法吗?

Maybe I don't understand threads well enough or maybe I am overlooking something simple. Anyone have any ideas?

谢谢!

推荐答案

据我所知,您正在使用 javax.smartcardio 包(直接或间接)来处理您的卡.某些线程(由您或您可能在 javax.smartcardio 之上使用的框架创建)调用了 Card 上的 beginExclusive() 方法实例以确保对卡的独占访问.

As far as I got you are using javax.smartcardio package (directly or indirectly) to work with your card. Some thread (created by you or by the framework you are probably using on top of javax.smartcardio) invoked beginExclusive() method on the Card instance to ensure exclusive access to the card.

独占访问是必要的,因为IC卡上保存的数据的处理是依赖于状态的,所以正确选择数据文件和读取它们的记录需要应用层的动作不受其他一些动作的干扰应用程序或线程.为此,存在这三个 Card 接口方法 beginExclusive()endExclusive()checkExclusive().

The exclusive access is necessary as treatment of the data kept on the IC cards is state-depended, so the proper selection of data files and reading of their records requires the actions of application layer not to be interfered with actions of some other application or thread. For this purpose these three Card interface methods beginExclusive(), endExclusive() and checkExclusive() exist.

所以你应该检查你的(框架)代码是否调用了 beginExclusive() 然后没有调用 endExclusive().

So you should check your(framework) code if it calls beginExclusive() and then doesn't call endExclusive().

这篇关于由另一个 Thread Java smartcardio 建立的独占访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:50