- BukkitTask aa= Bukkit.getScheduler().runTaskTimerAsynchronously(ZhiWumian.plugin,()->{
- Savezhiwudate.save();
- },0,ZhiWumian.plugin.getConfig().getLong("savetime")*20*60); 这种异步算分支线程还是主线程啊
Asynchronous tasks should never access any API in Bukkit. Great care should be taken to assure the thread-safety of asynchronous tasks.
Returns a task that will repeatedly run asynchronously until cancelled, starting after the specified number of server ticks.
——https://hub.spigotmc.org/javadoc ... TimerAsynchronously(org.bukkit.plugin.Plugin,java.lang.Runnable,long,long)
Javadoc 并没有说明是异步单线程还是异步多线程,但就「不要访问 Bukkit API」这个说法来看,应该是异步多线程。
本帖最后由 吕易天 于 2021-6-19 10:07 编辑
Async任务不是在主线程执行的
org.bukkit.craftbukkit.xxx.scheduler.CraftScheduler.java:
复制代码
org.bukkit.craftbukkit.xxx.scheduler.CraftAsyncScheduler.java:
复制代码
可见它用了线程池来执行任务
Async任务不是在主线程执行的
org.bukkit.craftbukkit.xxx.scheduler.CraftScheduler.java:
- @Override
- public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) {
- CraftScheduler.validate(plugin, runnable);
- if (delay < 0L) {
- delay = 0L;
- }
- if (period == 0L) {
- period = 1L;
- } else if (period < -1L) {
- period = -1L;
- }
- return this.handle(new CraftAsyncTask(this.asyncScheduler.runners, plugin, runnable, this.nextId(), period), delay);
- }
- protected CraftTask handle(CraftTask task, long delay) {
- if (!this.isAsyncScheduler && !task.isSync()) {
- this.asyncScheduler.handle(task, delay);
- return task;
- }
- task.setNextRun((long)this.currentTick + delay);
- this.addTask(task);
- return task;
- }
- protected void addTask(CraftTask task) {
- AtomicReference<CraftTask> tail = this.tail;
- CraftTask tailTask = tail.get();
- while (!tail.compareAndSet(tailTask, task)) {
- tailTask = tail.get();
- }
- tailTask.setNext(task);
- }
org.bukkit.craftbukkit.xxx.scheduler.CraftAsyncScheduler.java:
- private final ThreadPoolExecutor executor = new ThreadPoolExecutor(4, Integer.MAX_VALUE, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder().setNameFormat("Craft Scheduler Thread - %1$d").build());
- private final Executor management = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Craft Async Scheduler Management Thread").build());
- private final List<CraftTask> temp = new ArrayList<CraftTask>();
- @Override
- public void mainThreadHeartbeat(int currentTick) {
- this.currentTick = currentTick;
- this.management.execute(() -> this.runTasks(currentTick));
- }
- private synchronized void runTasks(int currentTick) {
- this.parsePending();
- while (!this.pending.isEmpty() && ((CraftTask)this.pending.peek()).getNextRun() <= (long)currentTick) {
- long period;
- CraftTask task = (CraftTask)this.pending.remove();
- if (this.executeTask(task) && (period = task.getPeriod()) > 0L) {
- task.setNextRun((long)currentTick + period);
- this.temp.add(task);
- }
- this.parsePending();
- }
- this.pending.addAll(this.temp);
- this.temp.clear();
- }
- private boolean executeTask(CraftTask task) {
- if (CraftAsyncScheduler.isValid(task)) {
- this.runners.put(task.getTaskId(), task);
- this.executor.execute(new ServerSchedulerReportingWrapper(task));
- return true;
- }
- return false;
- }
可见它用了线程池来执行任务