Remove the item stack reference thread
This commit is contained in:
parent
f1492cc829
commit
0f94634361
|
|
@ -135,6 +135,7 @@ public abstract class IngredientMixin implements ExtendedIngredient {
|
||||||
return stacks;
|
return stacks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
IngredientItemStacksSoftReference.clearReferences();
|
||||||
ItemStack[] result = computeItemsArray();
|
ItemStack[] result = computeItemsArray();
|
||||||
this.mfix$cachedItemStacks = new IngredientItemStacksSoftReference((Ingredient)(Object)this, result);
|
this.mfix$cachedItemStacks = new IngredientItemStacksSoftReference((Ingredient)(Object)this, result);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -11,28 +11,15 @@ public class IngredientItemStacksSoftReference extends SoftReference<ItemStack[]
|
||||||
private final Ingredient ingredient;
|
private final Ingredient ingredient;
|
||||||
|
|
||||||
private static final ReferenceQueue<ItemStack[]> QUEUE = new ReferenceQueue<>();
|
private static final ReferenceQueue<ItemStack[]> QUEUE = new ReferenceQueue<>();
|
||||||
private static final Thread DISCARD_THREAD = new Thread(IngredientItemStacksSoftReference::clearReferences, "Ingredient reference clearing thread");
|
|
||||||
|
|
||||||
static {
|
|
||||||
DISCARD_THREAD.setPriority(Thread.NORM_PRIORITY + 2);
|
|
||||||
DISCARD_THREAD.setDaemon(true);
|
|
||||||
DISCARD_THREAD.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IngredientItemStacksSoftReference(Ingredient ingredient, ItemStack[] stacks) {
|
public IngredientItemStacksSoftReference(Ingredient ingredient, ItemStack[] stacks) {
|
||||||
super(stacks, QUEUE);
|
super(stacks, QUEUE);
|
||||||
this.ingredient = ingredient;
|
this.ingredient = ingredient;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void clearReferences() {
|
public static void clearReferences() {
|
||||||
while (true) {
|
Reference<? extends ItemStack[]> ref;
|
||||||
Reference<? extends ItemStack[]> ref;
|
while ((ref = QUEUE.poll()) != null) {
|
||||||
try {
|
|
||||||
ref = QUEUE.remove();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ref instanceof IngredientItemStacksSoftReference ingRef && ingRef.ingredient instanceof ExtendedIngredient extIng) {
|
if (ref instanceof IngredientItemStacksSoftReference ingRef && ingRef.ingredient instanceof ExtendedIngredient extIng) {
|
||||||
// Null out the reference to the SoftReference object, to allow the SoftReference itself to be garbage collected.
|
// Null out the reference to the SoftReference object, to allow the SoftReference itself to be garbage collected.
|
||||||
extIng.mfix$clearReference();
|
extIng.mfix$clearReference();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user