First question here: it is a very short yet fundamental thing in Java that I don’t know…
In the following case, is the run() method somehow executed with the lock that somemethod() did acquire?
public synchronized void somemethod() {
Thread t = new Thread( new Runnable() {
void run() {
... <-- is a lock held here ?
}
}
t.start();
...
(lengthy stuff performed here, keeping the lock held)
...
}
No.
run()starts in its own context, synchronization-wise. It doesn’t hold any locks. If it did, you would either have a deadlock or it would violate the specs that state that only one thread may hold the lock on an object at any given time.If
run()was to callsomemethod()again on the same object, it would have to wait for thesomemethod()call that created it to complete first.