mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-15 07:05:52 +01:00
added new #tryAcquire method with a return value instead of consumer
This commit is contained in:
parent
72002a3d75
commit
8f6e0240eb
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user