Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过轮询检查智能卡是否存在_Java_Multithreading_Polling - Fatal编程技术网

Java 如何通过轮询检查智能卡是否存在

Java 如何通过轮询检查智能卡是否存在,java,multithreading,polling,Java,Multithreading,Polling,我需要检查Java应用程序中是否存在智能卡,以生成类似于智能卡移除“事件”的内容 我有一个简单的方法来测试它: public boolean isCardIn(){}; 最好的投票方式是什么? 在这种情况下,我应该使用java.utils.Timer.Timer()还是ExecutorService() 这是我当前的实施: 开始投票 checkTimer.schedule(new CheckCard(), delay,delay); 这是计时器的执行: private class Chec

我需要检查Java应用程序中是否存在智能卡,以生成类似于智能卡移除“事件”的内容

我有一个简单的方法来测试它:

public boolean isCardIn(){};
最好的投票方式是什么? 在这种情况下,我应该使用
java.utils.Timer.Timer()
还是
ExecutorService()


这是我当前的实施:

开始投票

checkTimer.schedule(new CheckCard(), delay,delay);
这是计时器的执行:

private class CheckCard extends TimerTask{   

    @Override
    public void run() {
        try{
            if(!SmartcardApi.isCardIn(slot)){
                 // fire event
            }
        }catch(Exception e){
        }
    }

}

我想再看看StackOverflow,因为我想你的问题已经得到了回答:

一般来说,我认为最好使用更新的API,在本例中是
ExecutorService
。我是这样做的:

主要方法 智能卡API 程序输出
在本例中,我使用了一个简单的
Runnable
,它可以调用另一个对象来触发它的
事件。您也可以使用
FutureTask
而不是此
Runnable
,但这只是基于您希望如何触发此事件的偏好。

我想将我的问题集中在轮询上。我已更改了答案,以显示您将如何使用它。至于原因,ExecutorService是一个较新的API。我了解到与系统时钟相关的
TimerTask
s及其执行时间可能存在问题,但我从未亲自使用过这两个问题。上面的代码片段只是使用
ExecutorService
IMO的一种方法。
public static void main(String[] args) throws InterruptedException, ExecutionException {
    ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    SmartCardApi smartCardApi = new SmartCardApi();

    // Polling job.
    Runnable job = new Runnable() {
        @Override
        public void run() {
            System.out.println("Is card in slot? " + smartCardApi.isCardInSlot());
        }
    };
    
    // Schedule the check every second.
    scheduledExecutor.scheduleAtFixedRate(job, 1000, 1000, TimeUnit.MILLISECONDS);
    
    // After 3.5 seconds, insert the card using the API.
    Thread.sleep(3500);
    smartCardApi.insert(1);
    
    // After 4 seconds, eject the card using the API.
    Thread.sleep(4000);
    smartCardApi.eject();

    // Shutdown polling job.
    scheduledExecutor.shutdown();
    
    // Verify card status.
    System.out.println("Program is exiting. Is card still in slot? " + smartCardApi.isCardInSlot());
}
package test;

import java.util.concurrent.atomic.AtomicBoolean;

public class SmartCardApi {
    private AtomicBoolean inSlot = new AtomicBoolean(false);
    
    public boolean isCardInSlot() {
        return inSlot.get();
    }
    
    public void insert(int slot) {
        System.out.println("Inserted into " + slot);
        inSlot.set(true);
    }

    public void eject() {
        System.out.println("Ejected card.");
        inSlot.set(false);
    }
}
Is card in slot? false
Is card in slot? false
Is card in slot? false
Inserted into 1
Is card in slot? true
Is card in slot? true
Is card in slot? true
Is card in slot? true
Ejected card.
Program is exiting. Is card still in slot? false