Skip to content

EddieChoCho/virtual-thread-refactor-helper

Repository files navigation

Virtual Thread Refactor Helper

Features

1. Refactor synchronized methods

  • Refactor non-static synchronized methods using a private ReentrantLock.

    • before refactor
      class Foo {
          private synchronized void hi() {
              // do something
          }
      }
    • after refactor
      class Foo {
          private ReentrantLock fooObjectLock = new ReentrantLock();
          private void hi() {
              fooObjectLock.lock();
              try{
                  // do something
              } finally{
                  fooObjectLock.unlock();
              }
          }
      }
  • Refactor static synchronized methods using a private static ReentrantLock.

    • before refactor
      class Foo {
          private static synchronized void hi() {
                // do something
          }
      }
    • after refactor
      class Foo {
          private static ReentrantLock fooClassLock = new ReentrantLock();
          private static void hi() {
              fooClassLock.lock();
              try{
                  // do something
              } finally{
                  fooClassLock.unlock();
              }
          }
      }

2. Refactor synchronized blocks

  • Refactor synchronized blocks that lock with the this object using a ReentrantLock.

    • before refactor
        class Foo {
            private void hi() {
                synchronized(this){
                    // do something
                }
            }
        }
    • after refactor
      class Foo {
          private ReentrantLock fooObjectLock = new ReentrantLock();
          private void hi() {
            {
              fooObjectLock.lock();
              try{
                  // do something
              } finally{
                  fooObjectLock.unlock();
              }
            }
          }
      }
  • Refactor synchronized blocks that lock with an object using a ReentrantLock.

    • before refactor
      class Foo {
          private Object object;
          private void hi() {
              synchronized(object){
                  // do something
              }
          }
      }
    • after refactor
      class Foo{
          private ReentrantLock objectLock = new ReentrantLock();
          private Object object;
          private void hi() {
            {
              objectLock.lock();
              try{
                  // do something
              } finally{
                  objectLock.unlock();
              }
            }
          }
      }
  • Refactor synchronized blocks that lock with a static object using a static ReentrantLock.

    • before refactor
      class Foo {
          private static Object object;
          private static void hi() {
              synchronized(object){
                  // do something
              }
          }
      }
    • after refactor
      class Foo{
          private static ReentrantLock objectLock = new ReentrantLock();
          private static Object object;
          private static void hi() {
            {
              objectLock.lock();
              try{
                  // do something
              } finally{
                  objectLock.unlock();
              }
            }
          }
      }
  • Refactor synchronized blocks that lock with a static/non-static component using a public ReentrantLock of the component.

    • before refactor
      class Boo {}
      class Foo {
          private Boo boo;
          private void hi() {
              synchronized(boo){
                  // do something
              }
          }
      }
    • after refactor
      class Boo {
          public ReentrantLock booObjectLock = new ReentrantLock();
      }
      class Foo{
          private Boo boo;
          private void hi() {
            {
              boo.booObjectLock.lock();
              try{
                  // do something
              } finally{
                  boo.booObjectLock.unlock();
              }
            }
          }
      }
  • Refactor synchronized blocks that lock with a class using a public static ReentrantLock of the class.

    • before refactor
      class Boo {}
      class Foo {
          private void hi() {
              synchronized(Boo.class){
                  // do something
              }
          }
      }
    • after refactor
      class Boo {
          public static ReentrantLock booObjectLock = new ReentrantLock();
      }
      class Foo{
          private Boo boo;
          private void hi() {
            {
              Boo.booObjectLock.lock();
              try{
                  // do something
              } finally{
                  Boo.booObjectLock.unlock();
              }
            }
          }
      }

TODO

  1. Improve features of refactor synchronized methods/blocks: refactor only if the methods/blocks contain any I/O operations.
  2. Add automation testing
  3. Add new features to refactor ThreadLocal usage with ScopedValue

References

About

Automated-refactoring for Java 21 Virtual Threads

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published