java: how to set a global thread-ID?

て烟熏妆下的殇ゞ 提交于 2019-12-06 10:51:24

If you want to have a globally unique request id, you can use the prcoessAndName (see below) with a counter and pass this with your message/request and use this to set the thread name.

public void processRequest(String uniqueRequestId, args) {
    Thread t = Thread.currrentThread();
    String tName = t.getName();
    try {
        t.setName("Processing " + uniqueRequestId);
        // preform process request
    } finally {
        t.setName(tName);
    }
}

This way the thread's name will contain the request id regardless of which thread/machine it is run in. If you include the thread's name in the logs, it will be included.


You can set the name of your thread using

String processAndHost = ManagementFactory.getRuntimeMXBean().getName()

You can append to this a descriptive name or counter.

This way all your thread names will be unique.

You can assume this name is globally unique provided, a) you have a unique hostname b) the process is not restarted so often there is a significant risk of two clients (at different times) with the same process id on the same host being a problem.

This can be combined with a locally unique id, to get a distributed unique id.

ExecutorService service = Executors.newCachedThreadPool(new ThreadFactory() {
    final String processName = ManagementFactory.getRuntimeMXBean().getName();
    final AtomicLong counter = new AtomicLong();
    @Override
    public Thread newThread(Runnable r) {
        String name = processName+"-"+counter.getAndIncrement();
        return new Thread(r, name);
    }
});
for(int i=0;i<10;i++)
    service.submit(new Runnable() {
        @Override
        public void run() {
            System.out.println("["+Thread.currentThread().getName()+"] - Hello World.");
            Thread.yield();
        }
    });
service.shutdown();

prints

[9480@myhost-0] - Hello World.
[9480@myhost-3] - Hello World.
[9480@myhost-2] - Hello World.
[9480@myhost-1] - Hello World.
[9480@myhost-5] - Hello World.
[9480@myhost-4] - Hello World.
[9480@myhost-6] - Hello World.
[9480@myhost-7] - Hello World.
[9480@myhost-0] - Hello World.
[9480@myhost-7] - Hello World.

You can give threads a name (see the different constructors of class java.lang.Thread). You could make up a name for each thread which consists of for example the computer name plus a unique number.

If you're creating threads using for example an ExecutorService, then you could pass that ExecutorService your own implementation of java.util.concurrent.ThreadFactory to make threads and set their name.

Not without unacceptable thread-creation overheads there isn't.

You should be logging by request-ID or session ID, not some impossible-to-implement global threadID that doesn't even exist.

I think you need to do some research on clicktrack logging.

You can set a name for the thread but you cant set/change an ID of a thread From java docs of Thread's getId method

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#getId()

"Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!