public final class TickSync extends Object
This class is necessary because plugins that are not following the Trident threading model can mutate and break the state consistency as described by the model. This is not good because plugins should not be expected to follow the threading model.
A demonstration of the state consistency being broken by conflicting thread-models:
WeatherConditions cnd = world.weather();
if (!cnd.isRaining()) { // If it is not raining
cnd.setRaining(true); // start raining
}
// Should return true!
boolean stillRaining = cnd.isRaining();
The stillRaining
variable can return false:
World Tick Thread ---> raining == false -----------------> raining = !true -> raining == false Plugin Thread -----> raining == false -> raining = true -----------------------> stillRaining == falseNot only do we not provide a set raining method, this class will be used to hold off plugin notification of server events until the entire tick has been processed.
Unless you want to severely mess up the server, do not call any methods in this class unless:
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG |
Modifier and Type | Method and Description |
---|---|
static void |
awaitSync()
Blocks the thread until this method is called again by a
complete(String) method |
static boolean |
canProceed()
Tests to see if the expected and update counters match
|
static void |
complete(String s)
Records that an update has occurred
|
static void |
increment(String s)
Increments the expected updates counter
|
static int |
left()
Obtains the tasks left in the queue
|
static Runnable |
next()
Obtains the next task in the queue
|
static void |
reset()
Resets the counters and the blocking mechanisms for the next tick iteration
|
static void |
sync(Runnable pluginTask)
Synchronizes the task for later execution once the tick completes
|
static Runnable |
waitForTask(long waitNanos)
Waits for a task to become available, or blocks until waitNanos has elapsed, in which case
null
will be returned |
public static volatile boolean DEBUG
public static void complete(String s)
Signals the main thread to sync method to continue if the expected and update counters match
public static boolean canProceed()
true
to indicate that the ticking can proceedpublic static void awaitSync()
complete(String)
methodpublic static void reset()
public static void sync(Runnable pluginTask)
pluginTask
- the taskpublic static Runnable waitForTask(long waitNanos) throws InterruptedException
null
will be returnedwaitNanos
- the nanos to wait for a tasknull
if there were noneInterruptedException
- if the current thread was interrupted in waiting for a taskpublic static Runnable next()
null
if there were nonepublic static int left()
Copyright © 2016. All rights reserved.