Java, how to get a caller from a separate thread?

Hmm ... not sure if this is possible, but how could you grab the caller's class name (and, ideally, the method name) in Java if you are working in a separate thread? I want to unload the class name received in a separate thread (so as not to block the UI thread when I do as 100+ registration operations per second).

eg:

class Main {
    public void callerMethod() {
        Thread.currentThread().getStackTrace()[2].getMethodName(); // This gets me the caller. However, this is expensive to do on the UI Thread 100+ times per second. a call here can take up 70ms

        new Thread(new Runnable() {
            @Override
            public void run() {
                new SecondObject().iWantToKnowMyCaller();
            }
        }).start();
    }
}



class SecondObject {
    public void iWantToKnowMyCaller() {
        // how do i get the caller method here so that it "callerMethod" from "Main" ?
    }
}

Usage example: I am recording a lot of data, and I do not want to block the main stream at all. some of the logs can be fast and small data, but some of them can record a lot of things. the problem is also that right now, as the code is written, callerMethod()there are about 600 input points, so refactoring will be a rather difficult task.

ALTERNATIVES:

, Thread.currentThread().getStackTrace()[2].getMethodName(); 5 , .

+4
3

EDIT:

, . : LogRecord. Logger.setSourceClassName() ( ), LogRecord .

public class ThreadTest
{
   public static void main( String[] args )
   {
      LogRecord lr = new LogRecord( Level.INFO, "Hi" );
      lr.setSourceClassName( "ThreadTest.main" );  // anything, including null
      Logger.getAnonymousLogger().log( lr );
   }
}

:

Thread , , . , , , . (P.S. . "2" callerMethod - "main" .)

public class ThreadTest
{
   public static void main( String[] args )
   {
      new Main().callerMethod();
   }
}

class Main {
    public void callerMethod() {
        final String callee = Thread.currentThread().getStackTrace()[1].getMethodName(); // This gets me the caller
        new MyThread(new Runnable() {
            @Override
            public void run() {
                new SecondObject().iWantToKnowMyCaller();
            }
        }){
        @Override
        public String getInvoker() { return callee; }}.start();
    }
}

abstract class MyThread extends Thread implements Invoker {    
   public MyThread( Runnable r )
   {
      super( r );
   }
}

class SecondObject {
    public void iWantToKnowMyCaller() {
        // how do i get the caller method here so that it "callerMethod" from "Main" ?
       System.out.println( ((MyThread)(Thread.currentThread())).getInvoker() );
    }
}

interface Invoker {
   String getInvoker();
}
+4
    public class ThreadTest{
        public static void main(String[] args) {
            new Main().callerMethod();
        }
    }
class Main{
    public void callerMethod() {
        new Thread(new MyRunnable(new Throwable())).start();
    }
    class MyRunnable implements Runnable {

        Throwable t;

        MyRunnable(Throwable t) {
            this.t = t;
        }

        @Override
        public void run() {
            StackTraceElement[] ses = t.getStackTrace();
            System.out.println(ses[0].getClassName() + ":" + ses[0].getMethodName() );
        }
    }
}
0

.

  • Thread. Thread.start(),, .
  • Runnable. , . , , , Runnable,, , , .
0

All Articles