All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.thread.Acquired Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.thread;

import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Represents an object that has been safely acquired and can be freed again.
 * 

* This class should not be shared, and it is recommended to call {@link #unlock()} * once the acquisition goal has been fulfilled to limit blocking time. * * @param the type of the acquired object */ public final class Acquired { private final T value; private final Thread owner; private final ReentrantLock lock; private boolean unlocked; Acquired(T value, TickThread tickThread) { this.value = value; this.owner = Thread.currentThread(); this.lock = AcquirableImpl.enter(owner, tickThread); } public @NotNull T get() { safeCheck(); return value; } public void unlock() { safeCheck(); this.unlocked = true; AcquirableImpl.leave(lock); } private void safeCheck() { Check.stateCondition(Thread.currentThread() != owner, "Acquired object is owned by the thread {0}", owner); Check.stateCondition(unlocked, "The acquired element has already been unlocked!"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy