-, . , ( java.util.concurrent (a ExecutorService)
synchronized , synchronized . , synchronized, (.. ), , :
final TextMessage msg =
Thread t = new Thread(r);
synchronized (msg) {
t.start();
}
r:
Runnable r = new Runnable() {
public void run() {
synchronized (msg) {
msg.setGpsLocation(findGpsLocation(msg));
}
}
}
TextMessage (.. - synchronized), , .
, synchronized , , (, , , , , ).
ExecutorService:
final TextMessage msg =
ExecutorService worker = Executors.newSingleThreadedExecutor();
Future<?> f = worker.submit(r);
r :
Runnable r = new Runnable() {
public void run() {
GpsLocation loc = findGpsLocation(msg);
msg.setGpsLocation(loc);
}
}
setGpsLocation synchronized ( getter , ). , , . , - , synchronize , :
synchronized (msg) {
if (msg.getGpsLocation().isIn(AMERICA))
msg.append(" DUDE!")
}