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

太原的网站建设公司哪家好广州建网站兴田德润可信

太原的网站建设公司哪家好,广州建网站兴田德润可信,怎么样给一个网站做横向导航栏,做电商网站搭建就业岗位介绍 我知道本地线程#xff0c;但直到最近才真正使用过它。 因此#xff0c;我开始深入研究该主题#xff0c;因为我需要一种传播某些用户信息的简便方法 通过Web应用程序的不同层#xff0c;而无需更改每个调用方法的签名。 小前提信息 线程是具有自己的调用栈的单个… 介绍 我知道本地线程但直到最近才真正使用过它。 因此我开始深入研究该主题因为我需要一种传播某些用户信息的简便方法 通过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.html
http://www.yutouwan.com/news/287048/

相关文章:

  • 网站推广策划的策略做网站seo的公司
  • 郑州市城乡建设局网站专门做投标书的网站
  • 程序员 给老婆做网站儿童教育网站源码
  • 免费制作永久个人网站网站开发后台指什么
  • 用asp做宠物网站页面小程序ui界面设计
  • flash网站设计师把照片做册子的网站
  • shopify做全品类网站一份完整的活动策划
  • 动易做网站松岗做网站
  • 查网站开发语言国家城乡建设部网站首页
  • 网站被黑了多久恢复17网站一起做网店池尾商圈
  • 网站开发项目个人总结图片在线制作生成器免费
  • 找外国男人做老公网站建设工程安全管理中心网站
  • 网站建设与管理的条件网站建设尺寸像素是多少钱
  • 临沂网站排名长春微网站
  • 网站为什么改版北京app网站建设价格
  • 手机网站模板cms电子商务网站建设需要做好哪些准备6
  • 网站开发制作入什么科目网站开发济南
  • 山西网站建设适合v加xtdseo网站漂浮特效怎么做
  • 兴平做网站政务网站建设需求
  • 深圳建网站哪个公司好网站移动站
  • 广州公司网站建设站优云网络公司
  • 那些做seo的网站查询个人证件证书查询
  • 网站制作公司 知乎wordpress批量添加摘要
  • 万网建网站小程序定制深圳
  • 网站网站设计宜昌网站建设兼职
  • 中石建基础设施建设有限公司网站成都龙泉建设局网站
  • 用dw做淘宝网站他达拉非是什么
  • 沽源网站建设宝安网站设计最好的公司
  • 确定网站设计公司简报苏州汇聚人电子科技有限公司
  • 常见网站模式网站建设1000zhu