Created
December 14, 2010 07:10
-
-
Save vaskoz/740100 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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