In my application, I need to perform multiple NFC-A tag reads with NfcA.transceive()
Also, the app needs to perform slow API calls associated with the different read data
Therefore, I am running Callables on separate threads to handle the various read->API tasks.
Is this a safe approach? The transceive comment string says that
This is an I/O operation and will block until complete.
Does this ensure that my calls to transceive (from different threads) will not interfere with each other?
Yes according to the docs other threads will wait, however you should be aware of the following:
public void close ()
Also causes all blocked I/O operations on other thread to be canceled and return with IOException
So if any thread calls close()
it will cause all other waiting threads to receive an IOException. How do you know which thread is the last one in order to call close()
correctly? And what happens if the tag moves out of range before all threads have finished?
I would propose an alternative approach, do all reading/transceive operations sequentially in a single thread, when each operation is finished put the data into a queue for another threadpool to process before moving on to the next transceive operation. This avoids blocking, is just as quick, easier to handle cleanup if the tag moves out of range, etc.