title: Getting the method that creates runnable from thread which runs the runnable
body: <p>Suppose I have a custom <code>Thread</code> class which is responsible for running runnables</p>
  
  <pre><code>public class MyThread extends Thread{
      public MyThread(Runnable r) {
          super(r);
      }
  
      @Override
      public void run() {
          super.run(); // Can I put something here to get info about where the runnable is submitted from?
      }
  }
  </code></pre>
  
  <p>Then in some method, I submit the runnable</p>
  
  <pre><code>public void someMethod() {
      new MyThread(new Runnable() {
          @Override
          public void run() {
              System.out.println("Blah");
          }
      });
  }
  </code></pre>
  
  <p>Suppose I have no control over <code>someMethod</code>, can I modify <code>MyThread</code> such that whenever a runnable is submitted to <code>MyThread</code>, I can get info about <code>someMethod</code> (e.g. method name <code>someMethod</code>, class name) ?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>In fact the original question is part of my problem.</p>
  
  <p>I am providing a <code>ThreadFactory</code> that can be used by a threadpool (ExecutorService).</p>
  
  <pre><code>public class MyThreadFactory implements ThreadFactory {
      @Override
      public Thread newThread(Runnable r) {
          return new MyThread(r);
      }
  }
  </code></pre>
  
  <p>User can create a threadpool with <code>MyThreadFactory</code></p>
  
  <p><code>ExecutorService pool = Executors.newCachedThreadPool(new MyThreadFactory());</code></p>
  
  <p>By calling pool.execute(runnable), a instance of MyThread will be created to perform the task specified by runnable. It is possible that the thread will be reused by multiple runnables. So I would like to retrieve <code>Method</code> and <code>Class</code> info in the <code>public void run()</code> method of <code>MyThread</code>. Since <code>runnable</code> is stored in a private field in the base <code>Thread</code> class, I cannot use a modified version of the solution provided by <strong>Laerte</strong> like:</p>
  
  <pre><code>@Override
  public void run() {
      super.run(); 
      // Not working, Since MyThread cannot access private field target of Thread class
      Method m = target.getClass().getEnclosingMethod();
      System.out.println(m.toString());
  }
  </code></pre>
  
  <p>Can I still obtain <code>Method</code> and <code>Class</code> about where the runnable is instantiated , at the moment <code>public void run()</code> is invoked?</p>