ThreadLocal?とは?

ThreadLocal?なオブジェクトの作り方

ThreadLocalMap?

ThreadLocalMap?のテストクラス

package com.snail;

import java.util.Map;


public class ThreadLocalExam {
	
	static Map<Object,Object> map = new ThreadLocalMap();

	public static void main(String[] args) {
		
		
		Child child1 = new Child("SAKURA");
		Child child2 = new Child("MOMO");
		Child child3 = new Child("KAEDE");
		
		child1.start();
		child2.start();
		child3.start();
	}
	
	static class Child extends Thread{
		private String name;
		
		public Child(String pName){
			name = pName;
		}
		
		@Override
		public void run(){
			
			System.out.println("THREAD-" + this.getId() + " SET \"" + name + "\" TO MAP");
			map.put("NAME", name);
			
			try {
				Thread.sleep(3000L);
			} catch (InterruptedException ignoreEx) {
				ignoreEx = null;
			}
			
			System.out.println("THREAD-" + this.getId() + " GET NAME FROM MAP : " + name );
		}
	}

}
 
THREAD-8  SET "SAKURA" TO MAP
THREAD-9  SET "MOMO" TO MAP
THREAD-10 SET "KAEDE" TO MAP

THREAD-8  GET NAME FROM MAP : SAKURA
THREAD-10 GET NAME FROM MAP : KAEDE
THREAD-9  GET NAME FROM MAP : MOMO

確かにスレッドローカルなMapになっているようだ

ServiceContext?(参考)

public class ServiceContext extends ThreadLocalMap{
  public Connection getConnection(){
    return (Connection)this.get("CONNECTION");
  }
  public void setConnection( Connection conn ){
    this.put("CONNECTION",conn);
  }
  public Sring getUserName(){
    return (String)this.get("USER_NAME");
  }
  public void setUserName( String userName ){
    this.put("USER_NAME",userName);
  }
  …
}

Java#JavaSE


*1 便利な日本語だ

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2007-01-15 (月) 02:19:19 (3610d)
ISBN10
ISBN13
9784061426061