ThreadLocal类

      通常在多线程中,当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本

      实现线程本地类其实不难:以当前线程为key,要保存的对象为value

      public class ThreadLocalSample {

           private Map map = Collections.synchronizedMap(new HashMap());

          

           public void set(Object value) {

               map.put(Thread.currentThread(), value);

           }

 

           public Object get() {

               Object value = map.get(Thread.currentThread());

               return value;

           }

      }

      ThreadLocal可用在Servlet的过滤器中,比如:每一个请求都由一个Connection来操作数据库,由于Servlet只有一个实例,所以应该是每个线程用一个连接,而不是所有线程用一个Connection,因此需要用到ThreadLocal类;由于Servlet是在Filter链的调用中点执行,即Filter1->Filter2->...->Servlet->Filter2->Filter1,因此:可以用一个Filter,在这个Filter调用下一个Filter前初始化连接,并将其放入ThreadLocal中,在调用又回到这个Filter时,得到连接并关闭。

      代码如下:

      public class TransactionManageFilter implements Filter {

              private FilterConfig config;

 

              public void init(FilterConfig config) throws ServletException {
                  this.config = config;

              }

 

              public void destroy() {
                 config = null;
              }

             
               public void doFilter(ServletRequest request, ServletResponse response,
                                              FilterChain chain) throws IOException, ServletException {

                      Connection conn = ConnectionManager.currentConnection();

                      try {

                            conn.setAutoCommit(false);

                            chain.doFilter(request, response, chain);

                            conn.commit();

                      } catch (Exception e) {

                            conn.rollback();

                      } finally {

                            try {

                               conn.setAutoCommit(true);

                               conn.close();

                               ConnectionManager.removeConnection();

                            } catch (Exception e) {}

                      }

               }

      }

 

      public class ConnectionManager {

          private static ThreadLocal currConn = new ThreadLocal();

      

          public static Connection currentConnection() {

              Object obj = currConn.get();

              if (obj != null) {

                 return (Connection)obj;

              } else {

                  Connection conn = ConnectionFactory.getConnection();

                  currConn.set(conn);

                  return conn;

              }

          }

 

         public static void removeConnection() {

             Object obj = currConn.get();

             if (obj != null) {

                currConn.set(null);

             }

        }

     }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值