亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

洪洞县| 铜鼓县| 安吉县| 聂荣县| 军事| 清苑县| 凤翔县| 西充县| 那坡县| 商河县| 龙口市| 奈曼旗| 松潘县| 关岭| 桂林市| 鄂托克旗| 年辖:市辖区| 宿松县| 神木县| 德安县| 婺源县| 砚山县| 榆中县| 新沂市| 平和县| 孟州市| 都昌县| 朝阳区| 西城区| 临沧市| 赣榆县| 奈曼旗| 千阳县| 松溪县| 恭城| 嵩明县| 沛县| 明水县| 宜宾县| 阿拉善左旗| 通化市|