如何在Java中获取线程ID

时间:2014-06-03 00:25:25

标签: java multithreading

我可以如下指定log4j格式化程序来打印spring mvc application

中的当前线程ID
log4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n

如何在spring应用程序中以编程方式获取当前线程ID。更确切地说,我想在拦截控制器方法的方面获取当前线程id。我的方面如下:

@Configurable
@Aspect
public class TimingAspect {

    @Autowired
    private HttpServletRequest httpServletRequest;

    //Generic performance logger for any mothod
    private Object logPerfomanceInfo(ProceedingJoinPoint joinPoint) {
       // do something with thread id
       // do something with httprequest
        ...
    }

    // Performance info for API calls
    @Around("execution(* package.controller.*.*(..))")
    public Object logAroundApis(ProceedingJoinPoint joinPoint) throws Throwable {
        return logPerfomanceInfo(joinPoint);
    }
}

1 个答案:

答案 0 :(得分:25)

这是一个严肃的问题,还是我根本不理解?

Thread.currentThread().getId()

我希望这就是你要找的东西。