当前位置: 首页 > news >正文

东海县做网站广告网站开发官网

东海县做网站广告,网站开发官网,清廉桂林网站,织梦装修设计网站模板threadlocal介绍 我知道本地线程#xff0c;但直到最近才真正使用过它。 因此#xff0c;我开始深入研究该主题#xff0c;因为我需要一种传播某些用户信息的简便方法 通过Web应用程序的不同层#xff0c;而无需更改每个调用方法的签名。 小前提信息 线程是具有自己的调… threadlocal 介绍 我知道本地线程但直到最近才真正使用过它。 因此我开始深入研究该主题因为我需要一种传播某些用户信息的简便方法 通过Web应用程序的不同层而无需更改每个调用方法的签名。 小前提信息 线程是具有自己的调用栈的单个进程。在Java中每个调用栈有一个线程或者每个线程有一个调用栈。即使您没有在程序中创建任何新线程线程也可以在没有您的程序的情况下运行最好的例子是当您仅通过main方法启动一个简单的Java程序时您没有隐式调用new Thread。start但是JVM为您创建了一个主线程以运行main方法。 主线程是非常特殊的因为它是所有其他线程都会从中生成的线程 线程完成后应用程序结束了它的生命周期。 在Web应用程序服务器中通常会有一个线程池因为创建的线程类非常重。所有JEE服务器WeblogicGlassfishJBoss等都有一个自调整线程池这意味着线程池会增加或减少需要的时间因此不会在每个请求上创建线程而现有的线程将被重用。 了解线程局部 为了更好地理解线程本地我将展示一种自定义线程本地的非常简单的实现。 package ccs.progest.javacodesamples.threadlocal.ex1;import java.util.HashMap; import java.util.Map;public class CustomThreadLocal {private static Map threadMap new HashMap();public static void add(Object object) {threadMap.put(Thread.currentThread(), object);}public static void remove(Object object) {threadMap.remove(Thread.currentThread());}public static Object get() {return threadMap.get(Thread.currentThread());}} 因此您可以随时在应用程序中调用CustomThreadLocal上的add方法该方法将在当前映射中将当前线程作为键并将要与该线程关联的对象作为值。 该对象可能是您想要从当前执行的线程中的任何位置访问的对象或者它可能是您想要与该线程保持关联并重复使用多次的昂贵对象。 您定义一个ThreadContext类您在其中拥有要在线程内传播的所有信息。 package ccs.progest.javacodesamples.threadlocal.ex1;public class ThreadContext {private String userId;private Long transactionId;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId userId;}public Long getTransactionId() {return transactionId;}public void setTransactionId(Long transactionId) {this.transactionId transactionId;}public String toString() {return userId: userId ,transactionId: transactionId;}} 现在是时候使用ThreadContext了。 我将启动两个线程并在每个线程中添加一个新的ThreadContext实例该实例将保存我想为每个线程传播的信息。 package ccs.progest.javacodesamples.threadlocal.ex1;public class ThreadLocalMainSampleEx1 {public static void main(String[] args) {new Thread(new Runnable() {public void run() {ThreadContext threadContext new ThreadContext();threadContext.setTransactionId(1l);threadContext.setUserId(User 1);CustomThreadLocal.add(threadContext);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();new Thread(new Runnable() {public void run() {ThreadContext threadContext new ThreadContext();threadContext.setTransactionId(2l);threadContext.setUserId(User 2);CustomThreadLocal.add(threadContext);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();} } 注意 CustomThreadLocal.addthreadContext是当前线程与ThreadContext实例相关联的代码行 正如您将看到执行此代码一样结果将是 userId:User 1,transactionId:1 userId:User 2,transactionId:2 这是怎么可能的因为我们没有将ThreadContextuserId或trasactionId作为参数传递给printThreadContextValues package ccs.progest.javacodesamples.threadlocal.ex1;public class PrintThreadContextValues {public static void printThreadContextValues(){System.out.println(CustomThreadLocal.get());} } 很简单 从CustomThreadLocal的内部映射调用CustomThreadLocal.get时将检索与当前线程关联的对象。 现在让我们看看何时使用真正的ThreadLocal类的示例。 上面的CustomThreadLocal类只是为了了解ThreadLocal类背后的原理该原理非常快并且以最佳方式使用内存 package ccs.progest.javacodesamples.threadlocal.ex2;public class ThreadContext {private String userId;private Long transactionId;private static ThreadLocal threadLocal new ThreadLocal(){Overrideprotected ThreadContext initialValue() {return new ThreadContext();}};public static ThreadContext get() {return threadLocal.get();}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId userId;}public Long getTransactionId() {return transactionId;}public void setTransactionId(Long transactionId) {this.transactionId transactionId;}public String toString() {return userId: userId ,transactionId: transactionId;} } 如javadoc所述ThreadLocal实例通常是希望将状态与线程关联的类中的私有静态字段。 package ccs.progest.javacodesamples.threadlocal.ex2;public class ThreadLocalMainSampleEx2 {public static void main(String[] args) {new Thread(new Runnable() {public void run() {ThreadContext threadContext ThreadContext.get();threadContext.setTransactionId(1l);threadContext.setUserId(User 1);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();new Thread(new Runnable() {public void run() {ThreadContext threadContext ThreadContext.get();threadContext.setTransactionId(2l);threadContext.setUserId(User 2);//here we call a method where the thread context is not passed as parameterPrintThreadContextValues.printThreadContextValues();}}).start();} } 调用get时 新的ThreadContext实例与当前线程关联然后将所需值设置为ThreadContext实例。 如您所见结果与第一组样本相同。 userId:User 1,transactionId:1 userId:User 2,transactionId:2 这可能是相反的顺序因此如果先看到“用户2”请不要担心 package ccs.progest.javacodesamples.threadlocal.ex2;public class PrintThreadContextValues {public static void printThreadContextValues(){System.out.println(ThreadContext.get());} } ThreadLocal的另一个非常有用的用法是当您有一个非常昂贵的对象的非线程安全实例时的情况。我发现的大多数极性示例是使用SimpleDateFormat但很快我将提供另一个使用Webservices端口的示例 package ccs.progest.javacodesamples.threadlocal.ex4;import java.text.SimpleDateFormat; import java.util.Date;public class ThreadLocalDateFormat {// SimpleDateFormat is not thread-safe, so each thread will have oneprivate static final ThreadLocal formatter new ThreadLocal() {Overrideprotected SimpleDateFormat initialValue() {return new SimpleDateFormat(MM/dd/yyyy);}};public String formatIt(Date date) {return formatter.get().format(date);} } 结论 线程局部变量有很多用途这里仅描述两种:(我认为使用最多的 真正的每线程上下文例如用户ID或事务ID。 每线程实例以提高性能。 参考 Java代码样本博客中的JCG合作伙伴 Cristian Chiovari 了解了ThreadLocal的概念 。 翻译自: https://www.javacodegeeks.com/2012/07/understanding-concept-behind.htmlthreadlocal
http://wiki.neutronadmin.com/news/208832/

相关文章:

  • 长沙网站建设 网站设计电影采集网站流量
  • 建设网站龙华教育网站解决方案
  • 网站开发工程师职业道德那个网站上有打码的任务做
  • 建筑网址大全网站企业主页的特点
  • 长沙专业外贸网站建设国内最新新闻事件摘抄
  • 那曲网站建设罗斯东莞房价一览表
  • shopify建站费用wordpress 用户 表单
  • 做流量网站要做哪一种网站代备案需要多少钱
  • 网站在线咨询代码常州快速建站模板
  • 做标书网站旅游景区网站建设方案文档
  • 河南省建设厅信息网站外贸管理软件有哪些
  • 做网站需要什么技术员广告公司业务员小刘与客户马经理
  • 一个空间怎么放2个网站如何自己开发app软件
  • 做网站框架显示不出来wordpress搬家
  • 济宁嘉祥网站建设哪里建网站最好
  • 广州市网站优化公司网站设计 广西
  • 电子商务网站建设的目标是什么意思小程序制作公司排名
  • 企业能建站吗建设wap手机网站
  • 网站建设维护 微信锦州建设信息网站
  • 郑州做网站设计东方网站建设
  • 甘肃网站定制开发大疫不过三年
  • 广告设计模板网站wordpress 推荐返利
  • 金华电子商务网站建设如何看别人网站用什么做的
  • 网站域名哪些后缀更好学院网站建设需求分析目录
  • 如何免费自己创建网站北京海淀区属于几环
  • 广东哪家网站建设哪家公司好株洲手机网站建设
  • 企业网站建设流程介绍太原网络营销外包
  • 自助网站建设程序旅行社的网站建设
  • 装修网站建设方案替代wordpress 搜索引擎
  • 克拉玛依网站建设公司客户型网站