Replace synchronized withReentrantLock#3715
Conversation
Performance metrics 🚀
|
| private boolean enabled; | ||
| private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock(); | ||
|
|
||
| public ActivityBreadcrumbsIntegration(final @NotNull Application application) { |
There was a problem hiding this comment.
To be honest I don't know why we had all the sychronized blocks in here, it seems to stem from here. I think we can remove it in future.
| public ActivityBreadcrumbsIntegration(final @NotNull Application application) { | |
| // TODO check if locking is even required at all for lifecycle methods | |
| public ActivityBreadcrumbsIntegration(final @NotNull Application application) { |
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
| // no-op | ||
| } |
There was a problem hiding this comment.
- I don't think we need the locking here
- Tell me how you automated that refactoring 😅
| try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | |
| // no-op | |
| } | |
| // no-op |
There was a problem hiding this comment.
I had to take a look at each thing I refactored manually 😢 . My thinking was to make this more clear in case we replace the noop comment with some actual code in the future.
There was a problem hiding this comment.
Replaced the locking code with a comment that shall serve as a reminder
| private final Collection<E> collection; | ||
| /** The object to lock on, needed for List/SortedSet views */ | ||
| final Object lock; | ||
| final AutoClosableReentrantLock lock; |
There was a problem hiding this comment.
could this be private as well?
There was a problem hiding this comment.
No because it's used in SynchronizedQueue. The same is basically true for any non final class since you have to use the right lock instead of just relying on the synchronized keyword on methods.
…ityBreadcrumbsIntegration.java Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
Instructions and example for changelogPlease add an entry to Example: ## Unreleased
- Replace `synchronized` with`ReentrantLock` ([#3715](https://github.com/getsentry/sentry-java/pull/3715))If none of the above apply, you can opt out of this check by adding |
📜 Description
synchronizedkeyword in method signatures andsynchronized (...) {blocks have been replaced by usingReentrantLockAutoClosableReentrantLockwhich offers anacquiremethod that locks and returns anAutoClosablethat unlocks onclose. This can be used intryblocksSynchronizedCollectionandSynchronizedQueuenow expect aAutoClosableReentrantLockinstead ofObjectwhen passing a lock objectsynchronizedmethods andsynchronized (this)) now uselockstatic synchronized,synchronized (...class)andsynchronized (...getInstance)) now usestaticLockObjecttoAutoClosableReentrantLock💡 Motivation and Context
Fixes #3312
💚 How did you test it?
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps