Java multiThread start/stop
Hej, jeg sidder og laver et program der kan aflæse NFC tags, og det fungere hidtil helt efter planenhar fået oprettet en ny thread til min NFC reader og den får læst og sent id på tagget når den finder en, mit problem er så nu at jeg skal kunne stoppe den igen
har prøvet med nfc.wait() hvor jeg så får IllegalMonitorStateException, og kan læse frem til at det er noget med syncronized block.
public Controller(MainGui gui) {
this.gui = gui;
program = new Program();
nfc = new NFCCardReader(this);
}
public void nfcScan() {
program.setCurrentUID(nfc.scan());
setText();
}
public void setText() {
gui.lblUID.setText(program.getCurrentUID());
gui.txaText.setText(program.getTagText());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gui.btnScan) {
if (gui.btnScan.getText() == "Scanning...") {
gui.btnScan.setText("Scan");
try {
nfc.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
gui.btnScan.setText("Scanning...");
nfc.start();
}
} else if (e.getSource() == gui.btnSave) {
program.setTag(gui.txaText.getText());
}
}
public class NFCCardReader extends Thread {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
private List<CardTerminal> terminals;
private Controller controller;
private boolean scanning;
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public NFCCardReader(Controller con) {
TerminalFactory factory = null;
try {
factory = TerminalFactory.getInstance("PC/SC", null);
System.out.println(factory);
terminals = factory.terminals().list();
controller = con;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CardException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
CardTerminal terminal = terminals.get(0);
scanning = true;
String uid = "";
while (scanning) {
try {
terminal.waitForCardPresent(0);
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
CommandAPDU command = new CommandAPDU(new byte[] { (byte) 0xFF,
(byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x04 });
ResponseAPDU response = channel.transmit(command);
byte[] byteArray = response.getBytes();
uid = bytesToHex(byteArray);
controller.setUID(uid);
} catch (CardException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String scan() {
if (terminals.isEmpty()) {
return "Ingen NFC terminaler tilgængelig";
}
CardTerminal terminal = terminals.get(0);
boolean looking = true;
String uid = "";
while (looking) {
try {
terminal.waitForCardPresent(0);
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
CommandAPDU command = new CommandAPDU(new byte[] { (byte) 0xFF,
(byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x04 });
ResponseAPDU response = channel.transmit(command);
byte[] byteArray = response.getBytes();
uid = bytesToHex(byteArray);
looking = false;
} catch (CardException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return uid;
}
}
Håber der er en som kan forstå hvad jeg mener og hjælpe til med en løsning