August 26, 2022

Hi,

I haven't tried, but can I do:

void foo(lots of params) {
  Object lock = (a particular condition) ? realLock : null;

  synchronized(lock) {
    // lots of complex code block here
  }

}

i.e depending on a a particular condition, the complex code block either need to be sync-protected, or not needed.

Method foo has lots of params, I try to avoid refactor the inner code block into a separate method.

So my question: is it possible synchronized(null)? essentially means NO-OP.

If it's not currently supported, can we add such feature?

Thank.

August 26, 2022
On the main forum, Paul Backus proposed a nested function as well as a scoped lock.

On 8/26/22 10:13, mw wrote:

>    Object lock = (a particular condition) ? realLock : null;

And I want to point out that "a particular condition" must not change between the check above and the following synchronized statement.

>
>    synchronized(lock) {
>      // lots of complex code block here
>    }

Ali