Thread-Local Storage模式

別名

  • 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();
    }
}

點(diǎn)我,12種Java多線程設(shè)計(jì)模式, 希望能幫到你

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容