Skip to content

Loom compatible - replace synchronized block with for example j.u.c.ReentrantLock #1951

@rbygrave

Description

@rbygrave

Loom project: https://openjdk.java.net/projects/loom/

With Loom there is a current known limitation that means code should ideally avoid performing IO inside a synchronized block.

The suggestion to make java code "loom friendly" is to replace the use of synchronized blocks with a java.util.concurrent.Lock (like ReentrantLock).

An example of the simple case is to replace code like:

void foo() {
  synchronized(this) {
     ... // performs IO or something blocking
  }
}

With code like:

// loom friendly ...

private final Lock lock = new ReentrantLock();

void foo() {
  lock.lock();
  try {
     ... // performs IO or something blocking
  } finally {
     lock.unlock();
  }
}

PgStatement simple case example

An example use of synchronized(this) in PgStatement is:
https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java#L244
https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java#L264

More difficult changes

e.g. PgStatement synchronized(connection)

There are more difficult changes in the case where for example PgStatement has synchronized on connection. In these cases there are more design options to consider.
https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgStatement.java#L906


  • Are we happy to prepare and submit PR's for the simple case replacement of sychronized(this) ?
  • Are we happy to find and replace code using sychronized(this) with code that uses ReentrantLock specifically or is there some other pattern/lock that people would prefer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions