【发布时间】:2019-07-18 03:48:56
【问题描述】:
对于我正在尝试的项目,通过按下按钮并在按下停止按钮时重新释放它来让 RFID 扫描仪一直运行,但是我试图在一个 while 循环中但问题似乎是,当我按下按钮代码一切正常,它确实进入一个循环但是在按下按钮后它不允许我按下其他任何东西,因为它卡在while循环中,有没有办法让它保持循环但同时能够用停止按钮停止它。
@Override
public void start() {
while (this.flag) {
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
CardTerminal terminal = terminals.get(0);
System.out.println("Waiting for a card..");
if (terminal == null) {
return;
}
terminal.waitForCardPresent(0);
Card card = terminal.connect("T=1");
System.out.println("Card: " + card);
System.out.println("Protocol: " + card.getProtocol());
CardChannel channel = card.getBasicChannel();
ResponseAPDU response = channel.transmit(new CommandAPDU(new byte[]{(byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00}));
System.out.println("Response: " + response.toString());
if (response.getSW1() == 0x63 && response.getSW2() == 0x00) {
System.out.println("Failed");
}
System.out.println("UID: " + bin2hex(response.getData()));
getUid = bin2hex(response.getData());
Thread.sleep(1000);
} catch (CardException e) {
System.out.println();
JOptionPane.showMessageDialog(null, "Device Not Connected " + e.getMessage());
} catch (InterruptedException ex) {
Logger.getLogger(CardId.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
对于我的开始按钮
t = new CardId();
t.start();
这是我的停止按钮
t.flag = false;
【问题讨论】:
-
我猜
CardId扩展了Thread -
是的,它是 CardId 类扩展线程
-
切题,扩展
Thread是一种有点“弃用”的并发方式,您至少应该使用Runnables 并传递给Thread的构造函数。 -
如果@Rogue 的回答不能帮助解决您的问题,那么您可能需要创建一个有效的minimal reproducible example,然后将此代码发布到您的问题中。为此,您需要将所有非核心库调用替换为模拟调用,例如
Thread.sleep以替换长时间运行的代码部分。这需要你付出相当多的努力才能做好,但如果需求很大,那么付出的努力可能是值得的。很幸运。
标签: java netbeans while-loop rfid