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

西安模板网站服务商站长统计app下载

西安模板网站服务商,站长统计app下载,拓展培训东莞网站建设,企业网站内容策划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.hkea.cn/news/14576538/

相关文章:

  • 怎么查询网站开发时间中卫网站建设
  • 网站职业技能培训班广州公司注册需要哪些资料
  • 自己建立网站用什么软件国内大型的网站建设
  • 山西网站的公司短网址工具
  • 网站建设工资一月多少钱wordpress 本地建站教程
  • 建设政协网站的意义哈尔滨做网站优化
  • 做电影网站被找版权问题怎么处理佛山 详情公布
  • 西安网站建设网络公司24小时免费更新在线视频
  • 网站程序购买淄博网络营销网站
  • 关于建设网站的通知品牌型网站成功案例图片
  • 云浮北京网站建设网络营销策划是什么
  • 成都网站排名优化小说网站开发背景
  • 微信公众号h5商城网站开发公司网站 备案
  • 新农村建设举报网站网站建设 作用
  • 外贸网站营销建站北京网站建设公司新闻
  • 建站服务公司网站源码wordpress orchard
  • 设计师去哪个网站找工作网站栏目设计规划表
  • 成都网站制作在线网页翻译在哪
  • 保定市住房和城乡建设厅网站网站推广有什么方法有哪些
  • 公司网站开发款记什么科目自媒体平台app下载
  • 郑州网站建设技术外包东莞注册公司需要什么资料
  • 宁波市建设厅网站首页射阳网站开发
  • 素材网网站建设自己做抽奖网站违法吗
  • 杭州市建设银行网站动漫设计培训班收费
  • 一个网站里面只放一个图片怎么做的wordpress卸载 数据库
  • 动态图片素材网站营销型网站重要性
  • 平台网站空间东莞网站建设页面设计
  • 建材在哪些网站做成都制作网站软件
  • 网站开发学什么专业网站目录是什么
  • 网站有风险提示怎么办兰州商城网站建