Skip to content

Commit

Permalink
feat(threads): added a new util class that helps with allocating threads
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Sep 23, 2023
1 parent 5886ac8 commit 4a0a11b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import com.seailz.discordjar.events.annotation.EventMethod;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.events.model.interaction.CustomIdable;
import com.seailz.discordjar.events.model.message.MessageCreateEvent;
import com.seailz.discordjar.utils.annotation.RequireCustomId;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.thread.DiscordJarThreadAllocator;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
}

method.setAccessible(true);
new Thread(() -> {
DiscordJarThreadAllocator.requestThread(() -> {
try {
method.invoke(listenerMethodPair.listener, event);
} catch (IllegalAccessException | ArrayIndexOutOfBoundsException e) {
Expand All @@ -103,7 +102,7 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
System.out.println(method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " threw an exception while being invoked.");
e.getCause().printStackTrace();
}
}).start();
}, "djar--EventDispatcher-inner").start();
}
}, "djar--EventDispatcher").start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.seailz.discordjar.utils.thread;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* Handles threading for discord.jar, just simplifies some things.
* @author Seailz
*/
public class DiscordJarThreadAllocator {

/**
* Requests a thread to be created and started. <b>All threads returned from this method are started automatically.</b>
* @param runnable The runnable to run on the thread.
* @return The thread that was created.
*/
public static Thread requestThread(Runnable runnable, String name) {
Thread thread = new Thread(runnable, name);
try {
thread.start();
} catch (OutOfMemoryError ex) {
Logger.getLogger("discord.jar-threading")
.severe("Out of memory error while starting thread " + name + "!");
printThreads();
throw ex;
}
return thread;
}

private static void printThreads() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

List<ThreadInfo> djarThreads = new ArrayList<>();
List<ThreadInfo> globalThreads = new ArrayList<>();
for(Long threadID : threadMXBean.getAllThreadIds()) {
ThreadInfo info = threadMXBean.getThreadInfo(threadID);
if (info.getThreadName().startsWith("djar--")) djarThreads.add(info);
globalThreads.add(info);
}

System.out.println("djar-thread-count: " + djarThreads.size());
for (ThreadInfo djarThread : djarThreads) {
System.out.println("djar-thread: " + djarThread.getThreadName());
}
System.out.println("global-thread-count: " + globalThreads.size());
}

}

0 comments on commit 4a0a11b

Please sign in to comment.