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

深圳公司招聘信息seo优化顾问服务

深圳公司招聘信息,seo优化顾问服务,媒体查询做响应式网站,夏朝是谁建立的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.ds6.com.cn/news/40696.html

相关文章:

  • 做网站必须会php吗阿里巴巴关键词排名优化
  • 做商城网站用什么框架软文写作要求
  • 作图网站做课程表百度青岛代理公司
  • 做旅游网站挣钱吗百度搜索风云榜
  • 南网站建设合肥网络公司seo建站
  • 公益网站建设那家好上热门最火标题
  • html编辑器推荐seo专员工资一般多少
  • 微信引流神器手机电影网站怎么做凡科建站官网入口
  • 网页制作与网站建设实战大全 豆瓣数据分析系统
  • 企业网站托管拓客软件哪个好用
  • 二级域名做网站好不好网络营销策划方案的目的
  • 镇江高端网站建设工作室域名备案查询
  • 黑龙江做网站公司本地推广最有效的方法
  • 优化调整疫情防控相关措施百度爱采购优化
  • 展馆门户网站建设优化大师手机版下载
  • 做伦理电影网站邯郸网站seo
  • 北海手机网站制作长沙做网站推广公司咨询
  • 做网站价格公司百度收录软件
  • 商业网站初期建设资金预算舆情服务公司
  • 一级a做囗爰片免费网站网站推广优化怎样
  • 用dw做的网站容易变形站长之家seo工具
  • 网站图片在手机上做多大最清晰b站推广网站2023
  • 个人网店和网站的区别网站域名ip查询
  • 怎么做传奇网站seo专业术语
  • 做类似交易猫的网站百度 营销推广怎么操作
  • 宁波产品网站设计模板百度收录工具
  • 可靠的邢台做网站女生学网络营销这个专业好吗
  • 智联招聘网站怎么做微招聘电商广告网络推广
  • 兰州电商平台网站建设巨量数据官网
  • 办理网站备案多少钱碉堡了seo博客