I'm new to multithreading and have some trouble synchronizing two threads.
I'm reading data from a tag which I then want to do calculations on in another thread, so my phone can continue communicating with the tag and not have to wait for the calculations to be done. The relevant code basically looks like this:
public void f() {
//...some stuff
for (byte[] data : dataObject) {
byte[] response = tag.transive(data); //exchange data with tag
CalculcationThread calc = new CalculationThread(data, card);
calc.start();
}
card.setReady(true);
wait(); //wait for all calculations to have finished
//do some stuff based on calculations
}
CalculationThread:
public class CalculationThread extends Thread {
...
@Override
public void run() {
//do calculations, then
if(card.isReady()) {
notify(); //notify main thread that all calculations are complete
}
}
}
I know that you need to put wait and notify in a synchorized block or add the synchronized tag to the method, but I still can't seem to figure out how to correclty synchronize these threads.
Thanks in advance.