中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java獲取線程ID的實例

發布時間:2020-10-14 10:48:23 來源:腳本之家 閱讀:147 作者:知優碼 欄目:開發技術

以下實例演示了如何使用 getThreadId() 方法獲取線程id:

public class Main extends Object implements Runnable {
 private ThreadID var;
 
 public Main(ThreadID v) {
  this.var = v;
 }
 
 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);
 }
 
 public static void main(String[] args) {
  ThreadID tid = new ThreadID();
  Main shared = new Main(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) {
  }
 }
}
 
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);
 }
}

以上代碼運行輸出結果為:

threadA: in initialValue()
threadA: var getThreadID =10001
threadB: in initialValue()
threadB: var getThreadID =10002
threadC: in initialValue()
threadC: var getThreadID =10003
threadA: var getThreadID =10001
threadB: var getThreadID =10002
threadC: var getThreadID =10003

以上就是Java獲取線程ID的實例的詳細內容,更多關于Java獲取線程ID的資料請關注億速云其它相關文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黄浦区| 安多县| 崇州市| 天祝| 杭州市| 集安市| 辽阳市| 贡觉县| 宜章县| 吴堡县| 涞水县| 汝州市| 康马县| 道真| 手机| 和平区| 昌图县| 衢州市| 佛山市| 城口县| 汝阳县| 休宁县| 和林格尔县| 洛浦县| 庆城县| 临朐县| 确山县| 固阳县| 盐源县| 福安市| 宁河县| 卓尼县| 政和县| 拉孜县| 金秀| 新野县| 察哈| 浮梁县| 安乡县| 赤城县| 信丰县|