Minestom wiki
Search…
Acquirable API

Presentation

Acquirable<T> represents an object of type T that you can retrieve but where its thread-safe access is not certain.
To give an example, imagine two entities very far from each other and therefore ticked in different threads. Let's imagine that entity A wants to trade items with entity B, it is something that would require synchronization to ensure that the trade happens successfully. From entity A's thread, you can retrieve a Acquirable<Entity> containing entity B, and from there acquire it to have safe access to the entity in a different thread.
The API provides multiple benefits:
  • Thread-safety with synchronous code
  • Same code whether you use one thread per chunk or a single for the whole server
  • Better control over your data to identify bottlenecks
Here how the acquirable API looks in practice:
1
Acquirable<Entity> acquirableEntity = ...;
2
System.out.println("Start acquisition...");
3
acquirableEntity.sync(entity -> {
4
// You can use "entity" safely in this consumer!
5
});
6
System.out.println("Acquisition happened successfully");
Copied!
Acquirable#acquire will block the current thread until acquirableEntity becomes accessible, and execute the consumer in the same thread once it is the case.
It is important to understand that the consumer is called in the same thread, it is the whole magic of the Acquirable API, your code stays the exact same.
The entity object from the consumer should however only be used inside of the consumer. Meaning that if you need to save the entity somewhere for further processing, you shall use the acquirable object instead of the acquired one.
1
private Acquirable<Entity> myEntity; // <- GOOD
2
// private Entity myEntity <- NO GOOD, DONT DO THAT
3
4
public void randomMethod(Acquirable<Entity> acquirableEntity) {
5
this.myEntity = acquirableEntity;
6
acquirableEntity.sync(entity -> {
7
// "myEntity = entity" is not safe, always cache the acquirable object
8
});
9
}
Copied!
Now, if you do not need the acquisition to happen synchronously you have the choice to create the request and handle it later on (at the end of the tick), for this you simply need to "schedule" the acquisition.
1
Acquirable<Entity> acquirableEntity = getAcquiredElement();
2
System.out.println("Hey I am starting the acquisition");
3
acquirableEntity.async(entity -> {
4
System.out.println("Hallo");
5
});
6
System.out.println("Hey I scheduled the acquisition");
Copied!
Will print you:
1
Hey I am starting the acquisition
2
Hey I scheduled the acquisition
3
Hallo
Copied!
And a few other options:
1
Acquirable<Player> acquirable = getAcquirable();
2
3
// #local() allows you to retrieve the element only if present
4
// in the current thread
5
Optional<Player> local = acquirable.local();
6
local.ifPresent(player -> {
7
// Run code...
8
});
9
10
// #lock() allows you to manually control when to unlock the acquisition
11
// if you cannot use #sync()
12
Acquired<Player> acquiredPlayer = acquirable.lock();
13
Player player = acquiredPlayer.get();
14
// Run code...
15
acquiredPlayer.unlock();
Copied!

Acquirable Collections

Let's say you have a AcquirableCollection<Player> and you want to access safely all of the players it contains. You have multiple solutions, each having pros & cons.

Naive loop

The one that you probably have in mind is:
1
// NAIVE ACQUIRABLE LOOP
2
AcquirableCollection<Player> acquirablePlayers = getOnlinePlayers();
3
for(Acquirable<Player> acquirablePlayer : acquirablePlayers){
4
acquirablePlayer.sync(player -> {
5
// Do something...
6
});
7
}
Copied!
It does work, but not efficient at all since you have to acquire each element one by one.

AcquirableCollection#forEachSync

It is the most efficient way to loop through a collection, the callback is executed for each individual element and stops only once all elements have been acquired.
1
AcquirableCollection<Player> acquirablePlayers = getOnlinePlayers();
2
acquirablePlayers.acquireSync(player -> {
3
// Do something...
4
});
5
acquirablePlayers.acquireAsync(player -> {
6
// Do something async...
7
});
Copied!
It is the most used one, simply because it doesn't create as much overhead as the previous ones. The element in the consumer is released directly without having to wait for the others.

Access the acquirable object without acquiring it

I can understand that having callbacks everywhere even if you know what you are doing is not ideal. You also have the choice to directly unwrap the acquirable object to retrieve the value inside.
1
Acquirable<Entity> acquirableEntity = ...;
2
Entity entity = acquirableEntity.unwrap();
Copied!
A similar method exists for collections.
1
AcquirableCollection<Player> acquirablePlayers = getOnlinePlayers();
2
Stream<Player> players = acquirablePlayers.unwrap();
Copied!
Those are not safe operations, be sure to read the Thread safety page to understand the implications.
I would personally recommend commenting everywhere you use those unsafe methods to indicate why this operation does not compromise the application's safety. If you cannot find any reason, you likely shouldn't.

Access all the entities in the current thread

1
Stream<Entity> entities = Acquirable.currentEntities();
Copied!

What type to request and return in methods

You have obviously the right to do what you prefer. But as a general rule, you should return Acquirable<T> objects and request for T.
The reason behind this choice is that you are sure (unless unsafe methods are used) that you will have safe access to the given parameter, but that you do not know what the user will want to do with it once you return.
Last modified 5mo ago