Servlet和ThreadLocal的测试

正确理解ThreadLocal http://www.iteye.com/topic/103804

总之,ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式。归纳了两点:
1。每个线程中都有一个自己的ThreadLocalMap类对象,可以将线程自己的对象保持到其中,各管各的,线程可以正确的访问到自己的对象。
2。将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这个对象作为参数传递的麻烦。

 

一个TheadLocal实例

下面,我们通过一个具体的实例了解一下ThreadLocal的具体使用方法。

代码清单2 SequenceNumber

package com.baobaotao.basic;

public class SequenceNumber {

①通过匿名内部类覆盖ThreadLocal的initialValue()方法,指定初始值

private static ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>(){

public Integer initialValue(){

return 0;

}

};

②获取下一个序列值

public int getNextNum(){

seqNum.set(seqNum.get()+1);

return seqNum.get();

}

public static void main(String[] args)

{

SequenceNumber sn = new SequenceNumber();

③ 3个线程共享sn,各自产生序列号

TestClient t1 = new TestClient(sn);

TestClient t2 = new TestClient(sn);

TestClient t3 = new TestClient(sn);

t1.start();

t2.start();

t3.start();

}

private static class TestClient extends Thread

{

private SequenceNumber sn;

public TestClient(SequenceNumber sn) {

this.sn = sn;

}

public void run()

{

for (int i = 0; i < 3; i++) {④每个线程打出3个序列值

System.out.println("thread["+Thread.currentThread().getName()+

"] sn["+sn.getNextNum()+"]");

}

}

}

}

 

通常我们通过匿名内部类的方式定义ThreadLocal的子类,提供初始的变量值,如例子中①处所示。TestClient线程产生一组序列号,在③处,我们生成3个TestClient,它们共享同一个SequenceNumber实例。运行以上代码,在控制台上输出以下的结果:

thread[Thread-2] sn[1]

thread[Thread-0] sn[1]

thread[Thread-1] sn[1]

thread[Thread-2] sn[2]

thread[Thread-0] sn[2]

thread[Thread-1] sn[2]

thread[Thread-2] sn[3]

thread[Thread-0] sn[3]

thread[Thread-1] sn[3]

考察输出的结果信息,我们发现每个线程所产生的序号虽然都共享同一个SequenceNumber实例,但它们并没有发生相互干扰的情况,而是各自产生独立的序列号,这是因为我们通过ThreadLocal为每一个线程提供了单独的副本。

 

 http://blog.csdn.net/qjyong/archive/2008/03/08/2158097.aspx

 

(madp把ThreadLocal看作Map)

 

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 public class TestThreadServlet extends HttpServlet {
        private static ThreadLocal thread = new ThreadLocal();

        private int flag = 0;
       
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
   flag++;
   String str = "This is the first String." + new Object();
   if (thread.get() == null)
   thread.set(str);
   PrintWriter out = response.getWriter();
   out.println("<p>");
   out.println("<BR>flag : " + flag);
   out.println("<BR>sessionid : " + request.getSession().getId());
   out.println("<BR>servlet : " + this.toString());
   out.println("<BR>thread : " + thread.get());
   out.println("</p>");
  }
   }

 


  执行结果:
  
  Session 1:
  
  flag : 2
  sessionid : amGeaiVwKvL9
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.Java.lang.Object@1ad6b4b
  
  Session 1:
  
  flag : 3
  sessionid : aR3GkcUQoXT-
  servlet : test.other.TestThreadServlet@5f2db0
  thread : This is the first String.java.lang.Object@6214f5
  
  由执行结果可以看出
  
  1 服务器对每个Servlet只创建一个实例。flag不停增加
  
  2 Session范围内的ThreadLocal中对象唯一。不同的请求,Object的hashCode相同。
  
  3 不同的Session共享ThreadLocal,但内部对象不同
  
  另:后来有人提醒我,实际上在web.XML为同一个servlet配置不同的名字,将会是两个不同的实例。也就是说,servlet的实例与配置有关。

你可能感兴趣的:(java,thread,servlet,Blog)