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

上海建设摩托车官方网站店铺详情页设计

上海建设摩托车官方网站,店铺详情页设计,百度推广创意范例,深圳美容网站建设ThreadLocal介绍 ThreadLocal为每个线程都提供了变量的副本#xff0c;使得每个线程访问各自独立的对象#xff0c;这样就隔离了多个线程对数据的共享#xff0c;使得线程安全。ThreadLocal有如下方法#xff1a; 方法声明 描述public void set(T value)设置当前线程绑定的…ThreadLocal介绍 ThreadLocal为每个线程都提供了变量的副本使得每个线程访问各自独立的对象这样就隔离了多个线程对数据的共享使得线程安全。ThreadLocal有如下方法 方法声明 描述public void set(T value)设置当前线程绑定的局部变量public T get()获取当前线程绑定的局部变量public void remove()移除当前线程绑定的局部变量protected Object initialValue()初始化值 ThreadLocal应用场景 场景一每个线程需要一个独享的对象典型的需要使用的类就是 SimpleDateFormat因为它是线程不安全的 package com.gingko.threadlocal;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;//线程不安全 public class DateUtils1 {private static SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);public static String formatDate(long seconds) {Date date new Date(seconds*1000);String format dateFormat.format(date);return format;}public static void main(String[] args) {ExecutorService executorService Executors.newFixedThreadPool(10);for(int i0;i100;i) {int finalI i;/*** 10个线程共享1个SimpleDateFormat会发生线程安全问题,运行结果出现相同的时间*/executorService.submit(()- {try {String formatDate formatDate(finalI);System.out.println(formatDate);Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}});}executorService.shutdown();} }运行结果 分析线程池创建了10个线程处理100个任务10个线程共享1个SimpleDateFormat发生了线程安全问题运行结果出现相同的时间。 改进版本加锁机制 package com.gingko.threadlocal;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //使用锁机制可以解决线程安全问题多线程时等待执行效率较低 public class DateUtils2 {private static SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);//使用锁机制多线程时等待执行效率较低public static synchronized String formatDate(long seconds) {Date date new Date(seconds*1000);String format dateFormat.format(date);return format;}public static void main(String[] args) {ExecutorService executorService Executors.newFixedThreadPool(10);for(int i0;i100;i) {int finalI i;/*** 10个线程共享1个SimpleDateFormat*/executorService.submit(()- {try {String formatDate formatDate(finalI);System.out.println(formatDate);Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}});}executorService.shutdown();} }运行结果 分析线程池创建了10个线程处理100个任务10个线程共享1个SimpleDateFormat在formatDate方法上加了锁使得多个线程同时执行此方法时需要排队等待获取锁执行结果没有问题但是由于要等待获取锁执行效率低。 改进版本使用ThreadLocal package com.gingko.threadlocal;import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //使用threadlocal,使得每个线程有各个独立的SimpleDateFormat public class DateUtils3 {//使用threadlocal,使得每个线程有各个独立的SimpleDateFormatprivate static ThreadLocalSimpleDateFormat dateFormatThreadLocal ThreadLocal.withInitial(()- {return new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);});public static String formatDate(long seconds) {Date date new Date(seconds*1000);String format dateFormatThreadLocal.get().format(date);return format;}public static void main(String[] args) {ExecutorService executorService Executors.newFixedThreadPool(10);for(int i0;i100;i) {int finalI i;/*** 10个线程有各自独立的SimpleDateFormat不会发生线程安全问题*/executorService.submit(()- {try {String formatDate formatDate(finalI);System.out.println(formatDate);Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}});}executorService.shutdown();} }运行结果 分析线程池创建了10个线程处理100个任务10个线程独占各自的SimpleDateFormat执行结果没有问题没有锁机制执行效率高。 场景二替代参数链传递 上图中通过前台获取到用户信息后一路向下传递假设方法A、B、C都需要用户信息一种方式是A、B、C方法都接收用户信息作为入参非常繁琐一种方式是将用户信息放入ThreadLocal中这样线程链上的所有方法都可以获取到用户信息不用在方法A、B、C显式的指定User Info的入参类似的应用场景还有前台传递的分页参数等。 package com.gingko.threadlocal;import com.gingko.entity.Student;public class TransferParam {//线程中放入student信息整个线程链路上都可以获取student信息private static ThreadLocalStudent studentThreadLocal new ThreadLocal();public static void main(String[] args) {TransferParam transferParam new TransferParam();transferParam.setParam();}public void setParam() {Student student new Student(1,张三,18,001);studentThreadLocal.set(student);new ServiceA().getParam();}class ServiceA {public void getParam() {Student student studentThreadLocal.get();System.out.println(ServiceA: student);new ServiceB().getParam();}}class ServiceB {public void getParam() {Student student studentThreadLocal.get();System.out.println(ServiceB: student);//线程链路的最后删除threadlocal信息防止发生内存泄露studentThreadLocal.remove();}} }运行结果 从结果上看出在方法setParam设置了ThreadLocal的变量student在其线程调用的链条上方法ServiceA.getParam 和ServiceB.getParam 都可以获取到student 注意在线程链最后的方法上记得调用ThreadLocal的remove方法不然会出现内存泄漏的风险这块内容后续的章节会介绍。
http://www.yayakq.cn/news/4458/

相关文章:

  • 台州网站策划台州网站策划写作平台有哪些
  • 网站建设图标图片湖北建设网
  • 网站打开很慢怎么做优化手机网站底部导航
  • 外贸soho怎么做网站做海外购网站
  • dw做网站首页怎么做网页设计与网站的关系
  • 罗湖区网站公司ui设计培训班需要学几个月
  • 网站建设评审验收会议主持词做企业网站和邮箱
  • 阿克苏网站开发牛魔王网站建设
  • 加强检察门户网站建设情况文档阅读网站模板下载
  • 做高仿网站网站建设与设计ppt模板
  • 厦门哪家做网站好电子商务网站开发环境示范
  • 周口网站设计网站做的一样算不算侵权
  • 网站建设提供书面资料清单Wordpress4.0参考手册.CHM
  • APP和网站是一样吗网站转化路径
  • 网站页面切换效果长春网站建设技术外包
  • 自己搭建服务器做网站网站网络排名优化方法
  • 如何做网站首页图电商发展趋势和未来
  • 网站备案查询工具xenforo和wordpress
  • 微网站首选公司淘宝客推广有效果吗
  • 短裙怎么做视频网站沧州国外网站建设
  • 网站域名是不是网址在线生成电子印章
  • ps做网站logo门户网站建站系统
  • 5个免费安全的资源网站建设通下载
  • 物联网技术应用专业是学什么的广州seo代理计费
  • 网站seo 优帮云wordpress网站搬家图片路径
  • 物流网站查询阅读的网站建设需要多少钱
  • 茂名专业网站建设太原网站推广公司
  • 网站被k的原因查内部券的网站是怎么做的
  • 网站域名变了能查吗建设银行官网首页网站南山片区
  • 什么样算网站需要备案瑞安市住房和城乡规划建设局网站