Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would create a thread that continously lists all running threads on the JVM.<br> Then each time it noticies that a new thread has appeared, it would notify in either way a class in your code.</p> <p>Here are some links about how to list all threads currently running on the JVM :</p> <ol> <li><p><a href="https://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java">Get a List of all Threads currently running in Java</a></p></li> <li><p><a href="http://www.exampledepot.com/egs/java.lang/ListThreads.html" rel="nofollow noreferrer">Listing All Running Threads</a></p></li> </ol> <p>============</p> <p>A starting code :</p> <p><strong>ThreadCreationListener.java</strong></p> <pre><code>public interface ThreadCreationListener { public void onThreadCreation(Thread newThread); } </code></pre> <p><strong>ThreadCreationMonitor.java</strong></p> <pre><code>public class ThreadCreationMonitor extends Thread { private List&lt;ThreadCreationListener&gt; listeners; private boolean canGo; public ThreadCreationMonitor() { listeners = new Vector&lt;ThreadCreationListener&gt;();//Vector class is used because many threads may use a ThreadCreationMonitor instance. canGo = true; // Initialize the rest of the class here... } // Most important methods public void addListener(ThreadCreationListener tcl) { listeners.add(tcl); } public void removeListener(ThreadCreationListener tcl) { listeners.remove(tcl); } public void run() { List&lt;Thread&gt; runningThreads; List&lt;Thread&gt; lastRunningThreads; while(canGo) { // Step 1 - List all running threads (see previous links) // runningThreads = ... // Step 2 - Check for new threads and notify all listeners if necessary if (runningThreads.removeAll(lastRunningThreads)==true) { for(Thread t : runningThreads) { for(ThreadCreationListener tcl : listeners) { tcl.onThreadCreation(t);//Notify listener } } } } } public void shutdown() { canGo = false; } </code></pre> <p>}</p> <p><strong>MyThreadInfoConsumer.java</strong></p> <pre><code>public class MyThreadInfoConsumer implements ThreadCreationListener { public void onThreadCreation(Thread newThread) { // Process here the notification... } } </code></pre> <p><strong>Main.java</strong></p> <pre><code>public class Main { public static void main(String[] args) { ThreadCreationMonitor tcm = new ThreadCreationMonitor(); tcm.start(); MyThreadInfoConsumer myTIC = new MyThreadInfoConsumer(); tcm.addListener(myTIC); // rest of your code... // Don't forget to call tcm.shutdown() when exiting your application ! } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload