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

深圳app网站网站流量统计查询

深圳app网站,网站流量统计查询,分类信息网站做淘客,帮企业做网站的ThreadLocal介绍 ThreadLocal为每个线程都提供了变量的副本,使得每个线程访问各自独立的对象,这样就隔离了多个线程对数据的共享,使得线程安全。ThreadLocal有如下方法: 方法声明 描述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 i=0;i<100;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 i=0;i<100;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 ThreadLocal<SimpleDateFormat> 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 i=0;i<100;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 ThreadLocal<Student> 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/595080/

相关文章:

  • 课程网站开发背景网站推广的几种方法
  • 商城网站建设模板一份完整的营销策划方案
  • 推广网站建设网站权重查询工具
  • t型布局网站怎么做建设网官方网站
  • 哪个建设网站推广竞价托管公司
  • 网站建设傲seo网站是什么意思
  • 卢氏住房和城乡建设厅网站聚名网
  • 山东网站建设电话长沙靠谱的关键词优化
  • 山东营销型网站ip网站查询服务器
  • 什么网站上做奥数题企业培训课程清单
  • 龙岩优化seo是什么意思
  • 外贸网站建设源码软文怎么做
  • 文章列表页wordpress宁波seo资源
  • 获取网站访客qq 原理百度投诉中心人工电话
  • 企业网站制作查询百度电话怎么转人工
  • 杭州专业网站建设怎样创建网站
  • 网站建设报价表格式淘宝关键词优化技巧
  • 高端网站建设系统百度网盘登录入口官网
  • ps做网站顶部江苏网络推广公司
  • 源码做网站手机网站百度关键词排名
  • 网站关键词分隔网站链接提交
  • 福永营销型网站多少钱中国最新消息今天
  • 做网站4000-262-263网站排名优化软件有哪些
  • 网站双链接怎么做网络舆情监测平台
  • 企业网站建设制作百度网盘下载app
  • asp做一个简单网站网络营销就是seo正确吗
  • 移动wap站点公司网站设计图
  • 网站策划建设seo搜索排名影响因素主要有
  • 大型商业广场网站建设互联网推广方案怎么写
  • p2vr做的网站上传网络广告策划书范文