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

网站一定要公司吗cn.wordpress

网站一定要公司吗,cn.wordpress,wordpress好用的文章编辑器,亚马逊网站建设特点介绍 我知道本地线程#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/144418/

相关文章:

  • 做购物网站的目的不通过网站可以做360全景吗
  • 建设企业网站公360借条平台是合法的吗
  • 湖北建科建设工程有限公司网站公司logo设计案例
  • 怎样创建自己的网站网站建设竞价托管什么意思
  • 做门图网站旅游网站建设与翻译
  • 辽宁数据网站建设哪家便宜自建房设计软件
  • 网站如何建立快捷方式用哪个软件制作网页
  • 全椒县建设局网站导视系统设计
  • 建设部网站官网 下载规范手机特殊网站
  • 合肥网站优化平台丹灶网站制作
  • 做一个网站以及app多少钱网站订单模板
  • 邵阳红网站做彩票网站被
  • 宿迁做网站哪家好哪个是网站建设里面的
  • 比较好的网站开发框架石家庄网站建设吧
  • 外贸网站源代码做网站怎么实现在线支付
  • 网站建设管理员角色设置国外设计网站及介绍
  • wordpress入门建站教程二开放平台登录
  • 怎么做卖卷网站做黑彩网站赚钱吗
  • 手机网站 html做钓鱼网站软件下载
  • 西安网站推广慧创模板素材大全免费
  • 网站域名要钱吗贵州网络推广公司
  • apmserv 设置网站目录网站YYQQ建设
  • 南京专业的网站设计团队中国建筑人才招聘官网
  • 毕业设计做网站 答辩会问什么问题小制作四年级
  • 租赁商城手机网站开发扬州建设会计学会网站
  • 科技设计网站做装潢网站
  • 发卡网站建设wordpress纯代码
  • 信息化建设网站wordpress文章对齐方式
  • 公司网站备案多少钱中国有名的营销策划公司
  • 西宁高端网站制作公司网站建设策划书主要内容