Java ThreadLocal的簡單例子

2021-07-03 01:33:05 字數 390 閱讀 8684

內容:

threadlocal:允許我們建立只能被同乙個執行緒讀寫的變數,例如web應用中將變數從前端到後台,並且需要在這次請求的執行緒中始終可以隨時獲取到。內部實現是通過乙個threadlocalmap這個map結構來實現的,將當前的threadlocal物件作為key,變數副本作為value。

public class testthreadlocal  catch (interruptedexception e) 

system.out.println(thread.currentthread() + " : " + threadlocal.get());

} }public static void main(string args)

}

Java Threadlocal類的使用和原理分析

threadlocal key value thread1 aaa thread2 bbb thread3 ccc 1 threadlocal api threadlocal類只有三個方法 void set t value 儲存值 t get 獲取值 void remove 移除值。2 thread...

簡單的單例

singleton 單例模式,保證乙個類僅有乙個例項,並提供乙個訪問它的全域性訪問點 乙個簡單的單例模式 class singleton public static singleton getsingleton return singleton 客戶端 public static void main...

cokies簡單用例

從提交的html表單中獲取,使用者名稱 string username request.getparameter username 以 username username 值 對 建立乙個cookie cookie theusername new cookie username username r...