Re:JVM Anatomy Park
by chrisseaton on Sunday January 07, 2018 @08:37PM
(#16094027)
But that's just a different example to the one you gave before. In your previous example acquiring the explicit lock always came before the start of synchronised block, both before and after the rewrite. You've changed it here so it's a different question.
[ Parent
| on HN ]
Re:JVM Anatomy Park
by zanker_swe on Sunday January 07, 2018 @08:56PM
(#16094080)
Sorry, I meant to write:
synchronized(this) {
a = AcquireLock();
for(c = 0; c < 100; c++) {
f();
}
}
ReleaseLock(a);
which is inline with what the blog post was proposing.To repeat the blog is a question:
for (...) {
synchronized (obj) {
// something
}
}
…could it optimize into this? synchronized (this) {
for (...) {
// something
}
}
My answer to that is in general, no.
Re:JVM Anatomy Park
by chrisseaton on Sunday January 07, 2018 @09:14PM
(#16094154)
You mean because ... could be code that can detect whether or not the monitor is held?
Yes, but I think it's an assumption so obvious as to be not worth stating that the author means as long as ... does not do that.
Re:JVM Anatomy Park
by zanker_swe on Sunday January 07, 2018 @09:31PM
(#16094226)
> My answer to that is in general, no.
Because '...' can include arbitrary side-effect inducing statements that can't be moved around without affecting the behavior. As the poster discovered.