From 229c493fbacfeb715187eeed64308707e434c1f5 Mon Sep 17 00:00:00 2001 From: l3r8yJ Date: Sun, 20 Aug 2023 22:02:34 +0300 Subject: [PATCH] reentrant lock --- src/main/java/org/cactoos/scalar/Synced.java | 28 +++++++++----------- src/main/java/org/cactoos/text/Synced.java | 12 ++------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/Synced.java b/src/main/java/org/cactoos/scalar/Synced.java index c480014e7..e558e2dca 100644 --- a/src/main/java/org/cactoos/scalar/Synced.java +++ b/src/main/java/org/cactoos/scalar/Synced.java @@ -24,6 +24,7 @@ package org.cactoos.scalar; import org.cactoos.Scalar; +import java.util.concurrent.locks.ReentrantLock; /** * Scalar that is thread-safe. @@ -50,37 +51,32 @@ public final class Synced implements Scalar { /** - * The scalar to cache. - */ - private final Scalar origin; - - /** - * Sync lock. + * The lock. */ - private final Object mutex; + private final ReentrantLock lock; /** - * Ctor. - * @param src The Scalar to cache + * The scalar to cache. */ - public Synced(final Scalar src) { - this(src, src); - } + private final Scalar origin; /** * Ctor. + * * @param scalar The Scalar to cache - * @param lock Sync lock */ - public Synced(final Scalar scalar, final Object lock) { + public Synced(final Scalar scalar) { this.origin = scalar; - this.mutex = lock; + this.lock = new ReentrantLock(); } @Override public T value() throws Exception { - synchronized (this.mutex) { + this.lock.lock(); + try { return this.origin.value(); + } finally { + this.lock.unlock(); } } } diff --git a/src/main/java/org/cactoos/text/Synced.java b/src/main/java/org/cactoos/text/Synced.java index c15ba4549..edd8f9195 100644 --- a/src/main/java/org/cactoos/text/Synced.java +++ b/src/main/java/org/cactoos/text/Synced.java @@ -33,20 +33,12 @@ * @since 0.18 */ public final class Synced extends TextEnvelope { - /** - * Ctor. - * @param text The text - */ - public Synced(final Text text) { - this(text, text); - } /** * Ctor. * @param text The text - * @param lck The lock */ - public Synced(final Text text, final Object lck) { - super(new TextOf(new org.cactoos.scalar.Synced<>(text::asString, lck))); + public Synced(final Text text) { + super(new TextOf(new org.cactoos.scalar.Synced<>(text::asString))); } }