public class Test {
ArrayList<X> xs;
ArrayList<Doer> doers;
Executor executor = Executors.newSingleThreadExecutor();
static class Doer {
public void does(X x){
x.f(); //
(2)
}
}
void test() {
for(X x : xs){
x.f(); //
(1)
for(Doer d : doers) {
executor.execute(() -> d.does(x));
}
}
}
}
For my eye, if X.f is not synchronized it is incorrect because of two facts
(and only that two facts):
1. Obviously, there is data race between (1) and (2). There are no more
data races here. (doers contains different objects)
2. There is no guarantee that (1) will be executed before (2). Yes?
If X.f would be synchronized that code will be correct because:
1. There is no data race.
2. There is guarantee that (1) will be executed before (2) because (1) is a
synchronization action and Executor.execute is also a synchronization
access (not specifically execute itself)
Yes?
--
You received this message because you are subscribed to the Google Groups
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.