-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Locks instead of synchronised keyword (#3077)
* Use Locks instead of synchronised keyword Closes #3040
- Loading branch information
1 parent
823799c
commit eb80671
Showing
26 changed files
with
285 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
testng-core-api/src/main/java/org/testng/internal/AutoCloseableLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.testng.internal; | ||
|
||
import java.io.Closeable; | ||
import java.util.Objects; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* A simple abstraction over {@link ReentrantLock} that can be used in conjunction with <code> | ||
* try..resources</code> constructs. | ||
*/ | ||
public final class AutoCloseableLock implements Closeable { | ||
|
||
private final ReentrantLock internalLock = new ReentrantLock(); | ||
|
||
public AutoCloseableLock lock() { | ||
internalLock.lock(); | ||
return this; | ||
} | ||
|
||
public boolean isHeldByCurrentThread() { | ||
return internalLock.isHeldByCurrentThread(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
internalLock.unlock(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
AutoCloseableLock that = (AutoCloseableLock) object; | ||
return Objects.equals(internalLock, that.internalLock); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(internalLock); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
testng-core-api/src/main/java/org/testng/internal/KeyAwareAutoCloseableLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.testng.internal; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* A simple abstraction over {@link java.util.concurrent.locks.ReentrantLock} that can be used when | ||
* we need to be dealing with a dictionary of lockable objects wherein we traditionally would have | ||
* used the <code>synchronized</code> keyword. | ||
*/ | ||
public final class KeyAwareAutoCloseableLock { | ||
private final Map<Object, AutoCloseableLock> internalMap = new ConcurrentHashMap<>(); | ||
|
||
public AutoReleasable lockForObject(Object key) { | ||
AutoCloseableLock internal = | ||
internalMap.computeIfAbsent(Objects.requireNonNull(key), k -> new AutoCloseableLock()); | ||
return new AutoReleasable(internal.lock(), () -> internalMap.remove(key)); | ||
} | ||
|
||
public static class AutoReleasable implements AutoCloseable { | ||
|
||
private final AutoCloseableLock lock; | ||
private final Runnable cleanupAction; | ||
|
||
AutoReleasable(AutoCloseableLock lock, Runnable cleanupAction) { | ||
this.lock = Objects.requireNonNull(lock); | ||
this.cleanupAction = | ||
this.lock.isHeldByCurrentThread() ? () -> {} : Objects.requireNonNull(cleanupAction); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
lock.close(); | ||
cleanupAction.run(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
AutoReleasable that = (AutoReleasable) object; | ||
return Objects.equals(lock, that.lock); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(lock); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.