-
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(); } } }
- before refactor
-
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(); } } }
- before refactor
-
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(); } } } }
- before refactor
-
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(); } } } }
- before refactor
-
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(); } } } }
- before refactor
-
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(); } } } }
- before refactor
-
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(); } } } }
- before refactor
- Improve features of refactor synchronized methods/blocks: refactor only if the methods/blocks contain any I/O operations.
- Add automation testing
- Add new features to refactor ThreadLocal usage with ScopedValue