假设我正在synchronized某个线程内执行一个代码块,并且在synchronized块内我调用一个方法,该方法产生另一个线程来处理需要与第一个方法相同锁定的同步代码块.所以在伪Java代码中:
public void someMethod() {
synchronized(lock_obj) {
// a whole bunch of stuff...
// this is the last statement in the block
(new Thread(someOtherMethod())).start();
}
// some more code that doesn't require a lock
}
public void someOtherMethod() {
// some setup code that doesn't require a lock
// return the stuff we want to run in another thread
// that does require a lock
return new Runnable() {
@Override
public void run() {
synchronized(lock_obj) { …Run Code Online (Sandbox Code Playgroud)