6多线程
Bukkit 的线程调度器接口
测试代码:
建议使用异步操作
配置文件:
stuck:
description: 执行同步或异步的线程任务
usage: /<command> [1|2|3...]
命令实现:
/**
* @author houyunfei
*/
public class ThreadCommandExecutor implements CommandExecutor {
/**
* 在Spigot 插件里 运行 同步/异步 任务的两种方式
* 1. 继承BuukkitRunnable 抽象类,重写run方法 然后调用BuukkitRunnable#runTask()方法
* 2. 使用BukkitScheduler#runTask()方法
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if ("stuck".equalsIgnoreCase(label)) {
if (args != null && args.length > 0) {
Plugin plugin = MyPlugin.instance;
String arg = args[0];
sender.sendMessage("案例" + arg);
switch (arg) {
case "1":
// 同步任务1
executeSyncTask1(sender);
break;
case "2":
executeSyncTask2(sender);
break;
case "3":
executeSyncTask3(sender);
break;
case "4":
executeSyncTask4(sender);
break;
case "5":
executeSyncTask5(sender);
break;
default:
break;
}
}
}
return true;
}
private void executeSyncTask5(CommandSender sender) {
/**
* 本示例同效于 示例3
*/
sender.sendMessage("当前时刻:" + System.currentTimeMillis() + ",你将会在3s后 收到下一条提示。");
// 线程调度器
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskLaterAsynchronously(MyPlugin.instance, new Runnable() {
@Override
public void run() {
sender.sendMessage("这是3s后的消息,当前时间戳:" + System.currentTimeMillis());
}
}, 3 * 20);
}
private void executeSyncTask4(CommandSender sender) {
BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
sender.sendMessage("当前时刻:" + System.currentTimeMillis() + ",3s后异步执行的任务已经完成。");
}
};
/**
* 参数一:当前插件
* 参数二:几ticks后执行
* 参数三:几ticks后循环执行
*
* 返回值:BukkitTask
*/
BukkitTask bukkitTask = task.runTaskTimerAsynchronously(MyPlugin.instance, 0, 20);
// 这里必须用到BukkitTask返回值了,如果不主动改变isCancelled状态,该runnable会被一直重复调用下去
Bukkit.getScheduler().runTaskLater(MyPlugin.instance, () -> {
bukkitTask.cancel();
}, 4 * 20);
}
/**
* 3s后 异步 地执行run()函数
*/
private void executeSyncTask3(CommandSender sender) {
sender.sendMessage("当前时刻:" + System.currentTimeMillis() + ",你将会在3s后收到下一条提示。");
BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
sender.sendMessage("当前时刻:" + System.currentTimeMillis() + ",3s后异步执行的任务已经完成。");
}
};
task.runTaskLaterAsynchronously(MyPlugin.instance, 3 * 20);
}
/**
* 异步执行,虽然依旧是3s,但是BukkitScheduler会异步的调用该runnable任务,此时玩家仍然能得到服务器的响应。
*/
private void executeSyncTask2(CommandSender sender) {
BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
sender.sendMessage("虽然这里Thread.sleep了3s,但是该任务被异步执行了,并不会影响主线程,此时你仍然能收到服务器的响应。");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
sender.sendMessage("主线程测试结束");
}
}
};
task.runTaskAsynchronously(MyPlugin.instance);
}
private void executeSyncTask1(CommandSender sender) {
BukkitRunnable task = new BukkitRunnable() {
@Override
public void run() {
sender.sendMessage("接下来3s,主线程会被卡死,你在服务器任何操作都不会响应");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
sender.sendMessage("主线程被卡死结束");
}
}
};
task.runTask(MyPlugin.instance);
}
}