別名
- Per-Thread Attribute
- Thread-Specific Data
- Thread-Specific Field
- Thread-Local Storage
適用的情況
使每個(gè)線程擁有獨(dú)立的上下文實(shí)例. 從而避免了多線程之間的實(shí)例競(jìng)爭(zhēng).
實(shí)現(xiàn)的方式
java.lang.ThreadLocal來(lái)保管相關(guān)的所有線程的單獨(dú)實(shí)例.
代碼示例:
Log利用ThreadLocal保存著所有線程單獨(dú)的TSLog對(duì)象, 然后每個(gè)線程通過(guò)TSLog打印各自的日志到各自本地的日志文件.
package com.graphic.threadSpecificStorage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class TSLog {
private PrintWriter printWriter = null;
public TSLog(String fileName) {
try {
printWriter = new PrintWriter(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
public void println(String s) {
printWriter.write(s);
}
public void close() {
printWriter.write("=== END OF Log ===");
printWriter.close();
}
}
package com.graphic.threadSpecificStorage;
public class Log {
private static final ThreadLocal<TSLog> tsLogCollection = new ThreadLocal<TSLog>();
public static void println(String s) {
getTSLog().println(s);
}
public static void close() {
getTSLog().close();
}
private static TSLog getTSLog() {
TSLog tsLog = tsLogCollection.get();
if (tsLog == null) {
tsLog = new TSLog(Thread.currentThread().getName() + "-log.txt");
tsLogCollection.set(tsLog);
}
return tsLog;
}
}
package com.graphic.threadSpecificStorage;
public class ClientThread extends Thread {
public ClientThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println(getName() + " Begin");
for (int i = 0; i < 10; i++) {
Log.println("i = " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.close();
System.out.println(getName() + " END");
}
}
package com.graphic.threadSpecificStorage;
public class Main {
public static void main(String[] args) {
new ClientThread("alice").start();
new ClientThread("bob").start();
new ClientThread("chris").start();
}
}