added new #tryAcquire method with a return value instead of consumer

This commit is contained in:
TheMode 2021-04-21 14:32:42 +02:00
parent 72002a3d75
commit 8f6e0240eb

View File

@ -5,6 +5,7 @@ import net.minestom.server.thread.BatchThread;
import net.minestom.server.thread.ThreadProvider;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
@ -83,6 +84,21 @@ public final class Acquirable<T> {
return false;
}
/**
* Retrieves {@link #unwrap()} only if this element can be safely
* acquired without any synchronization.
*
* @return this element or null if unsafe
*/
public @Nullable T tryAcquire() {
final Thread currentThread = Thread.currentThread();
final BatchThread elementThread = getHandler().getBatchThread();
if (Objects.equals(currentThread, elementThread)) {
return unwrap();
}
return null;
}
/**
* Signals the acquisition manager to acquire 'this' at the end of the thread tick.
* <p>