Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created December 14, 2010 07:10
Show Gist options
  • Save vaskoz/740100 to your computer and use it in GitHub Desktop.
Save vaskoz/740100 to your computer and use it in GitHub Desktop.
# JRuby
require 'java'
VirtualMachine = com.sun.tools.attach.VirtualMachine
vm = VirtualMachine.attach("4567") # target JVM process ID
vm.loadAgent("/tmp/ThreadAssassin.jar") # path to JAR with Agent code.
vm.detach
public class SomeThreadedApp {
public static void main(String[] args) {
new Thread(new BoringRunner(), "bRunner1").start();
new Thread(new BoringRunner(), "bRunner2").start();
new Thread(new BoringRunner(), "bRunner3").start();
}
static class BoringRunner implements Runnable {
public void run() {
int count = 1;
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
System.out.println("Thread named " + Thread.currentThread().getName()
+ " and count is: " + count);
count++;
}
}
}
}
import java.lang.instrument.Instrumentation;
public class ThreadAssassin {
public static void agentmain(String agentArgs, Instrumentation inst) {
ThreadGroup root = Thread.currentThread().getThreadGroup();
while (root.getParent() != null) {
root = root.getParent();
}
Thread[] allThreads = new Thread[root.activeCount()];
root.enumerate(allThreads);
System.out.println("The assassin takes aim!");
for (Thread t : allThreads) {
if (t.getName().startsWith("bRunner")) {
System.out.println("Thread named: " + t.getName() + " is gonna die!");
t.stop(); // I know this is deprecated and bad; just for effect.
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment