thread-specific stroage模式 一個線程一個儲物櫃

定義:

thread-specific stroage模式 是一種即使只有一個入口,也會在內部爲每個線程分配特定的空間的模式

引例:

不用thread-specific stroage模式 的實例

public class Log {
    private static PrintWriter writer = null;

    // 初始化writer字段
    static {
        try {
            writer = new PrintWriter(new FileWriter("log.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 加入一筆log
    public static void println(String s) {
        writer.println(s);
    }

    // 關閉log
    public static void close() {
        writer.println("==== End of log ====");
        writer.close();
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println("BEGIN");
        for (int i = 0; i < 10; i++) {
            Log.println("main: i = " + i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
        Log.close();
        System.out.println("END");
    }
}

 用thread-specific stroage模式 的實例

public class Log {
    private static final ThreadLocal tsLogCollection = new ThreadLocal();

    // 加入一個log
    public static void println(String s) {
        getTSLog().println(s);
    }

    // 關閉log
    public static void close() {
        getTSLog().close();
    }

    // 取得線程特有的log
    private static TSLog getTSLog() {
        TSLog tsLog = (TSLog)tsLogCollection.get();

        //如果線程是第一次呼叫,就建立新擋案並登陸log
        if (tsLog == null) {
            tsLog = new TSLog(Thread.currentThread().getName() + "-log.txt");
            tsLogCollection.set(tsLog);
        }

        return tsLog;
    }
}
public class TSLog {
    private PrintWriter writer = null;

    //  初始化writer字段
    public TSLog(String filename) {
        try {
            writer = new PrintWriter(new FileWriter(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //  加入一筆log
    public void println(String s) {
        writer.println(s);
    }

    //  關閉log
    public void close() {
        writer.println("==== End of log ====");
        writer.close();
    }
}
public class ClientThread extends Thread {
    public ClientThread(String name) {
        super(name);
    }
    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) {
            }
        }
        Log.close();
        System.out.println(getName() + " END");
    }
}
public class Main {
    public static void main(String[] args) {
        new ClientThread("Alice").start();
        new ClientThread("Bobby").start();
        new ClientThread("Chris").start();
    }
}

 

thread-specific stroage模式 出現的角色:

client(委託者):client角色將處理委託給tsobjectporxy角色

tsobjectporxy(線程特有的對象的代理人):tsobjectporxy角色會執行多個client角色委託給他處理

tsobjectcollection(線程特有的對象集合):tsobjectcollection角色有一張client與client角色對應表。

tsobject(線程特有的對象):tsobject角色保存着線程特有的信息。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章