国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 中ThreadLocal 的正確用法

java 中ThreadLocal 的正確用法

2020-09-02 09:56Java教程網 Java教程

這篇文章主要介紹了java 中ThreadLocal 的正確用法的相關資料,需要的朋友可以參考下

javaThreadLocal 的正確用法

用法一:在關聯數據類中創建private static ThreadLocalThreaLocal的JDK文檔中說明:ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread。如果我們希望通過某個類將狀態(例如用戶ID、事務ID)與線程關聯起來,那么通常在這個類中定義private static類型的ThreadLocal 實例。

例如,在下面的類中,私有靜態 ThreadLocal 實例(serialNum)為調用該類的靜態 SerialNum.get() 方法的每個線程維護了一個“序列號”,該方法將返回當前線程的序列號。(線程的序列號是在第一次調用 SerialNum.get() 時分配的,并在后續調用中不會更改。)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class SerialNum {
  // The next serial number to be assigned
  private static int nextSerialNum = 0;
 
  private static ThreadLocal serialNum = new ThreadLocal() {
    protected synchronized Object initialValue() {
      return new Integer(nextSerialNum++);
    }
  };
 
  public static int get() {
    return ((Integer) (serialNum.get())).intValue();
  }
}

【例】

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ThreadContext {
 
 private String userId;
 private Long transactionId;
 
 private static ThreadLocal threadLocal = new ThreadLocal(){
  @Override
    protected ThreadContext initialValue() {
      return new ThreadContext();
    }
 
 };
 public static ThreadContext get() {
  return threadLocal.get();
 }
 
 public String getUserId() {
  return userId;
 }
 public void setUserId(String userId) {
  this.userId = userId;
 }
 public Long getTransactionId() {
  return transactionId;
 }
 public void setTransactionId(Long transactionId) {
  this.transactionId = transactionId;
 }
 
}

 用法二:在Util類中創建ThreadLocal

這是上面用法的擴展,即把ThreadLocal的創建放到工具類中。

【例】例如hibernate的工具類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class HibernateUtil {
  private static Log log = LogFactory.getLog(HibernateUtil.class);
  private static final SessionFactory sessionFactory;   //定義SessionFactory
 
  static {
    try {
      // 通過默認配置文件hibernate.cfg.xml創建SessionFactory
      sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
      log.error("初始化SessionFactory失敗!", ex);
      throw new ExceptionInInitializerError(ex);
    }
  }
 
  //創建線程局部變量session,用來保存Hibernate的Session
  public static final ThreadLocal session = new ThreadLocal();
 
  /**
   * 獲取當前線程中的Session
   * @return Session
   * @throws HibernateException
   */
  public static Session currentSession() throws HibernateException {
    Session s = (Session) session.get();
    // 如果Session還沒有打開,則新開一個Session
    if (s == null) {
      s = sessionFactory.openSession();
      session.set(s);     //將新開的Session保存到線程局部變量中
    }
    return s;
  }
 
  public static void closeSession() throws HibernateException {
    //獲取線程局部變量,并強制轉換為Session類型
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
      s.close();
  }
}

用法三:在Runnable中創建ThreadLocal

 還有一種用法是在線程類內部創建ThreadLocal,基本步驟如下:

1、在多線程的類(如ThreadDemo類)中,創建一個ThreadLocal對象threadXxx,用來保存線程間需要隔離處理的對象xxx。

2、在ThreadDemo類中,創建一個獲取要隔離訪問的數據的方法getXxx(),在方法中判斷,若ThreadLocal對象為null時候,應該new()一個隔離訪問類型的對象,并強制轉換為要應用的類型。

3、在ThreadDemo類的run()方法中,通過調用getXxx()方法獲取要操作的數據,這樣可以保證每個線程對應一個數據對象,在任何時刻都操作的是這個對象。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class ThreadLocalTest implements Runnable{
  
  ThreadLocal<Studen> studenThreadLocal = new ThreadLocal<Studen>();
 
  @Override
  public void run() {
    String currentThreadName = Thread.currentThread().getName();
    System.out.println(currentThreadName + " is running...");
    Random random = new Random();
    int age = random.nextInt(100);
    System.out.println(currentThreadName + " is set age: " + age);
    Studen studen = getStudent(); //通過這個方法,為每個線程都獨立的new一個student對象,每個線程的的student對象都可以設置不同的值
    studen.setAge(age);
    System.out.println(currentThreadName + " is first get age: " + studen.getAge());
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println( currentThreadName + " is second get age: " + studen.getAge());
    
  }
  
  private Studen getStudent() {
    Studen studen = studenThreadLocal.get();
    if (null == studen) {
      studen = new Studen();
      studenThreadLocal.set(studen);
    }
    return studen;
  }
 
  public static void main(String[] args) {
    ThreadLocalTest t = new ThreadLocalTest();
    Thread t1 = new Thread(t,"Thread A");
    Thread t2 = new Thread(t,"Thread B");
    t1.start();
    t2.start();
  }
  
}
 
class Studen{
  int age;
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/vking_wang/article/details/14225379

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国外爱爱视频 | 午夜激情视频在线观看 | 国产精品三级久久久久久电影 | 91欧美激情一区二区三区成人 | 亚洲综合精品 | 日日噜| 性色av一区二区三区红粉影视 | 亚洲综合中文网 | 91免费视频| 中文色视频 | 国产欧美精品区一区二区三区 | 在线免费av观看 | 亚洲视频区 | a级片在线观看 | 国产精品久久久久久一区 | 欧美一区二区三区在线视频 | 在线中文字幕视频 | 成人精品一区二区三区中文字幕 | 日本久久久久久久久久 | 国产影视 | 中文在线日韩 | 99国产精品久久久久久久久久 | 日韩av免费看 | 亚洲精品国产乱码在线看蜜月 | 国内久久精品 | 97久久精品午夜一区二区 | 欧美日韩国产一区二区 | 国产在线一区不卡 | 日韩精品一二三 | 狠狠操影院| 欧洲视频一区 | 国产亚洲精品美女久久久久久久久久 | 奇米在线| 午夜在线观看视频 | 欧美精品v国产精品v日韩精品 | 欧美成人激情视频 | 亚洲视频自拍 | 97久久精品午夜一区二区 | 国产成人精品一区二区在线 | 天天操天操 | 国产精品永久免费自在线观看 |