Core Concepts
Types of Schedulers:
syncScheduler
- Executes tasks on the main thread, ideal for interacting with the main game/server logic.
asyncScheduler
- Executes tasks on a separate thread, used for tasks that are resource-intensive or should not block the main thread.
scheduler
- A generic interface for managing both sync and async tasks based on user preferences or system needs.
Common Methods
after(delay, timeUnit)
- Schedules a task to execute after a delay.
- Arguments:
delay
: The amount of time to wait.timeUnit
: Unit of time (e.g.,TimeUnit.SECONDS
).
every(interval, timeUnit)
- Schedules a repeating task.
- Arguments:
interval
: Time between consecutive executions.timeUnit
: Unit of time.
run(action)
- Executes the defined action.
- Arguments:
action
: ARunnable
or method reference (e.g.,this::taskMethod
).
Examples
Synchronous Scheduler (syncScheduler)
Example 1: Run a Task After Delay
BasePlugin.syncScheduler()
.after(5, TimeUnit.SECONDS)
.run(() -> System.out.println("Task executed on the main thread after 5 seconds."));
Example 2: Repeating Task
BasePlugin.syncScheduler()
.every(10, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running on the main thread every 10 seconds."));
Use Case:
- Modify the game state or server settings safely on the main thread.
Asynchronous Scheduler (asyncScheduler)
Example 1: Background Task Execution
BasePlugin.asyncScheduler()
.after(3, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Heavy task started asynchronously.");
// Simulate heavy computation
});
Example 2: Repeated Background Task
BasePlugin.asyncScheduler()
.every(5, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running asynchronously every 5 seconds."));
Use Case:
- Handle I/O operations or computational tasks without blocking the main thread.
Generic Scheduler (scheduler)
Example 1: Conditional Task Type
boolean useAsync = true;
BasePlugin.scheduler(useAsync)
.after(2, TimeUnit.SECONDS)
.run(() -> System.out.println("This task runs on " + (useAsync ? "an async thread." : "the main thread.")));
Example 2: Dynamic Task Type for Repeating Jobs
boolean isAsync = false;
BasePlugin.scheduler(isAsync)
.every(3, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task " + (isAsync ? "asynchronously" : "synchronously")));
Advanced Use Cases
- Chaining Multiple Tasks
BasePlugin.syncScheduler()
.after(5, TimeUnit.SECONDS)
.run(() -> System.out.println("First task executed."))
.after(7, TimeUnit.SECONDS)
.run(() -> System.out.println("Second task executed 2 seconds later."));
- Handling Task Cancellation
ScheduledTask task = BasePlugin.asyncScheduler()
.every(1, TimeUnit.SECONDS)
.run(() -> System.out.println("Repeating task running."));
BasePlugin.syncScheduler()
.after(10, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Cancelling the repeating task after 10 seconds.");
task.cancel();
});
- Combining Sync and Async Tasks
BasePlugin.asyncScheduler()
.after(2, TimeUnit.SECONDS)
.run(() -> {
System.out.println("Async task running. Switching to sync task...");
BasePlugin.syncScheduler()
.run(() -> System.out.println("Sync task running after async task!"));
});
- Delayed Execution with Conditions
BasePlugin.syncScheduler()
.after(3, TimeUnit.SECONDS)
.run(() -> {
if (Math.random() > 0.5) {
System.out.println("Condition met. Task executed.");
} else {
System.out.println("Condition not met. Task skipped.");
}
});
- Long-Running Tasks (Split into Smaller Steps)
BasePlugin.asyncScheduler()
.every(1, TimeUnit.SECONDS)
.run(new Runnable() {
private int step = 0;
@Override
public void run() {
if (step >= 5) {
System.out.println("Task complete.");
return; // Task ends after 5 iterations
}
System.out.println("Executing step " + (++step));
}
});
Comparison of Schedulers
Feature | syncScheduler | asyncScheduler | scheduler |
---|---|---|---|
Thread | Main thread | Worker thread | Configurable (sync/async) |
Use Case | Game/server logic | Background tasks | Depends on user preference |
Performance | Limited by main thread | Non-blocking, faster | Flexible |
Task Repetition | Supported | Supported | Supported |
Chaining | Supported | Supported | Supported |
That's all