I have two threads running from a controller class. The first thread receives SMS messages and should continue running as long as the program is in the started state. The other thread is used to calculate the units GPS location.
The controller launches the SMS thread and waits from a text message. If a text message meets certain criteria, the GPS location thread is launched and coordinates are sent back to the controller.
For each thread, I've used the following format:
reader = new Reader(this);
new Thread(reader).start();
The reader class then uses a reference of the controller so it can call a method in the controller:
public void ReceivedCommand(String address) {
[..]
}
This method then creates an instance of the GPS thread which itself calls a method from the parent object (thread?) called ReceivedLocation
which then sets up the new SMS message (TextMessage
object). The problem is that the SMS thread can only return the original sender's address (to reply to) and I need to use the GPS thread so I can set the Payload for the SMS message.
So now I have 2 methods using the same object (TextMessage
object), but I want to ensure that the first method (SMS address setter) doesn't change the address while the GPS thread is getting the GPSLocation to set.
Can synchronizing a block within ReceivedCommand()
:
- Add the address to the
TextMessage
object, - Run the GPS thread
- Let the GPS thread call the second method (
ReceivedLocation()
) - And let that method change the TextMessage object?