从 ListenerAdapter 创建线程会阻止它监听事件 Java JDA
Creating Thread from ListenerAdapter stops it from listening for events Java JDA
您好,我正在使用这个 JDA Discord Bot,但我在线程方面遇到了一些困难。我的主要机器人 class 扩展了 ListenerAdapter,我希望它侦听一个命令,然后启动一个预定服务以每周一次 post 一条消息。问题是当创建线程时它开始执行,但主要 class 停止监听。
想知道我是否应该创建一个内部 class 即 ListenerAdapter 并将它们都放在线程中,或者我是否缺少其他解决方案。我在大学做过一些线程方面的工作,但经验不多。
public class Main extends ListenerAdapter {
static Thread SER;
public static void main(String[] args) throws LoginException {
...
}
@Override
public void onMessageReceived(MessageReceivedEvent event){
if(event.getMessage().getContentRaw().equals("!startmessages")){
try {
SER = new Thread(new ScheduledExecutorRepeat(event.getChannel(),event.getGuild()));
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(SER);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Creating messages");
}
}
class ScheduledExecutorRepeat implements Runnable {
private int count = 0;
MessageChannel channel;
Guild guild;
ScheduledExecutorService ses;
ScheduledFuture<?> scheduledFuture;
public ScheduledExecutorRepeat(MessageChannel mc,Guild g) throws InterruptedException {
channel = mc;
guild = g;
ses = Executors.newScheduledThreadPool(1);
run();
}
public void run(){
Runnable task1 = () -> {
count++;
System.out.println("Running...Weekly Message - count : " + count);
};
// init Delay = 5, repeat the task every 1 second
scheduledFuture = ses.scheduleAtFixedRate(task1, 5, 1000*60*60*24*7, TimeUnit.SECONDS);
while (true) {
System.out.println("count :" + count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
程序卡在了 ScheduledExecutorRepeat class 末尾的循环中。删除 while 循环解决了这个问题。预定的消息有效,机器人仍在收听消息。
您好,我正在使用这个 JDA Discord Bot,但我在线程方面遇到了一些困难。我的主要机器人 class 扩展了 ListenerAdapter,我希望它侦听一个命令,然后启动一个预定服务以每周一次 post 一条消息。问题是当创建线程时它开始执行,但主要 class 停止监听。
想知道我是否应该创建一个内部 class 即 ListenerAdapter 并将它们都放在线程中,或者我是否缺少其他解决方案。我在大学做过一些线程方面的工作,但经验不多。
public class Main extends ListenerAdapter {
static Thread SER;
public static void main(String[] args) throws LoginException {
...
}
@Override
public void onMessageReceived(MessageReceivedEvent event){
if(event.getMessage().getContentRaw().equals("!startmessages")){
try {
SER = new Thread(new ScheduledExecutorRepeat(event.getChannel(),event.getGuild()));
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(SER);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Creating messages");
}
}
class ScheduledExecutorRepeat implements Runnable {
private int count = 0;
MessageChannel channel;
Guild guild;
ScheduledExecutorService ses;
ScheduledFuture<?> scheduledFuture;
public ScheduledExecutorRepeat(MessageChannel mc,Guild g) throws InterruptedException {
channel = mc;
guild = g;
ses = Executors.newScheduledThreadPool(1);
run();
}
public void run(){
Runnable task1 = () -> {
count++;
System.out.println("Running...Weekly Message - count : " + count);
};
// init Delay = 5, repeat the task every 1 second
scheduledFuture = ses.scheduleAtFixedRate(task1, 5, 1000*60*60*24*7, TimeUnit.SECONDS);
while (true) {
System.out.println("count :" + count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
程序卡在了 ScheduledExecutorRepeat class 末尾的循环中。删除 while 循环解决了这个问题。预定的消息有效,机器人仍在收听消息。