ThreadID.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- public class ThreadID extends ThreadLocal {
- private int nextID;
- public ThreadID() {
- nextID = 10001;
- }
- private synchronized Integer getNewID() {
- Integer id = new Integer(nextID);
- nextID++;
- return id;
- }
- // override ThreadLocal's version
- protected Object initialValue() {
- print("in initialValue()");
- return getNewID();
- }
- public int getThreadID() {
- // Call get() in ThreadLocal to get the calling
- // thread's unique ID.
- Integer id = (Integer) get();
- return id.intValue();
- }
- private static void print(String msg) {
- String name = Thread.currentThread().getName();
- System.out.println(name + ": " + msg);
- }
- }