Java 获取线程id

2021-03-20  本文已影响0人  西贝巴巴
package com.company;

/*

 */
public class GetThreadTest extends Object implements Runnable {
    private ThreadID var;

    public GetThreadTest(ThreadID v) {
        this.var = v;
    }

    public static void main(String[] args) {
        ThreadID tid = new ThreadID();
        GetThreadTest shared = new GetThreadTest(tid);
        try {
            Thread threadA = new Thread(shared, "threadA");
            threadA.start();

            Thread.sleep(500);

            Thread threadB = new Thread(shared, "threadB");
            threadB.start();

            Thread.sleep(500);

            Thread threadC = new Thread(shared, "threadC");
            threadC.start();
        } catch (InterruptedException x) {
        }
    }

    public void run() {
        try {
            print("var getThreadID =" + var.getThreadID());
            Thread.sleep(2000);
            print("var getThreadID =" + var.getThreadID());
        } catch (InterruptedException x) {
        }
    }

    private static void print(String msg) {
        String name = Thread.currentThread().getName();
        System.out.println(name + ": " + msg);
    }
}


class ThreadID extends ThreadLocal {
    private int nextID;

    public ThreadID() {
        nextID = 10001;
    }

    private synchronized Integer getNewID() {
        Integer id = new Integer(nextID);
        nextID++;
        return id;
    }

    protected Object initialValue() {
        print("in initialValue()");
        return getNewID();
    }


    public int getThreadID() {
        Integer id = (Integer) get();
        return id.intValue();
    }

    private static void print(String msg) {
        String name = Thread.currentThread().getName();
        System.out.println(name + ": " + msg);
    }
}
上一篇下一篇

猜你喜欢

热点阅读