Reputation: 4291
I need to get Thread names and log them in AspectJ class. This solution does't work
`@Around("call(void method*())")
public Object condition(ProceedingJoinPoint joinPoint) throws Throwable
{
PropertyConfigurator.configure("log4j.properties");
long before = System.currentTimeMillis();
Object res = joinPoint.proceed();
long diff = System.currentTimeMillis() - before;
logger.info(Thread.currentThread().getName() + "\t" + diff );
return res;
}
` because the result is e.g.:
main 717
My aspect is around
some methods. How to get name of thread where method is executed (not thread created by aspect)?
Upvotes: 1
Views: 1721
Reputation: 67417
I have not tried to reproduce the threading situation, but I guess another approach might be more helpful in your situation: You could combine your around
advice with introduction (a.k.a. inter-type declarations). Declare a member storing the thread information you want to extract in the Runnable
classes of interest or maybe in the classes called by them and access that information in your logging advice.
Edit: You might want to use a ThreadLocal
for storing the information. ;-)
Edit 2: Have you tried to use an execution()
pointcut instead of a call()
pointcut in order to be in the executing context and probably get the running thread directly without any tricks?
Upvotes: 0
Reputation: 33544
1. First of all its Not the thread where method is execute, But the thread that executes the method.
2. Here what you can do to identify the thread that executed that method:
- Initialize the thread.
Thread t = new Thread(MyClass);
// MyClass is the class which extends or implements Thread or Runnable respectively.
- Naming the thread of execution.
t.setName("T1");
- Inside the method getting the thread that is executing it.
public void go(){
Thread.currentThread().getName();
}
Upvotes: 1
Reputation: 7899
Where ever you are creating thread set their name in the format you want like below otherwise it will give the default assigned name.
public static void main(String[] args) {
System.out.println("Thread : "+Thread.currentThread().getName());
Thread.currentThread().setName("My Main Thread");
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread : "+Thread.currentThread().getName());
}
});
t1.setName("t1 thread");
System.out.println("Thread : "+Thread.currentThread().getName());
t1.start();
}
Result:
Thread : main
Thread : My Main Thread
Thread : t1 thread
Upvotes: 0
Reputation: 3170
it seams for me that the current thread of execution is main thread which is currentThread. If you want to dump all threads, use this:
public static String getDumpFor(Thread thread) {
StringBuilder st = new StringBuilder();
if (thread.isAlive()) {
StackTraceElement[] stackTrace = thread.getStackTrace();
st.append(thread.toString()).append("\n")
.append(String.format(" State - %s,", thread.getState()))
.append(String.format(" Is daemon = %s,", thread.isDaemon()));
for (StackTraceElement s : stackTrace)
st.append("\tat ").append(s.getClassName()).append(".").append(s.getMethodName()).append("(").append(s.getFileName()).append(":").append(s.getLineNumber()).append(")")
.append("\n");
} else {
st.append(" No thread ");
}
return st.toString();
}
/**
* print thread dump for each active thread over logger API.
*/
public static void dumpActiveThreads() {
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
Set<Thread> keySet = stackTraces.keySet();
System.out.println("\nThread dump begin:");
for (Thread thread : keySet)
dumpActiveThread(thread);
System.out.println("\nThread dump end.");
}
However i still may missunderstood the question "How to get real Thread name?"...
Upvotes: 0
Reputation: 4114
**You can setName() method**
//get currently running thread object
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
/*
* To set name of thread, use
* void setName(String threadName) method of
* Thread class.
*/
currentThread.setName("Set Thread Name Example");
public void run() {
System.out.println(th.getName()+" is starting.....");
System.out.println("Current thread name : "+
Thread.currentThread().getName());
}
Upvotes: 0