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

大浪网站建设/怎么在百度上设置自己的门店

大浪网站建设,怎么在百度上设置自己的门店,Wordpress球队网站,网站页面模板页面布局1.简单介绍 在ThreadLocal类中有一个Map,用于存储每一个线程的变量副本,Map中元素的键为线程对象,而值对应线程的变量副本。 下面是示例: public class ThreadLocalTest {/*1通过匿名内部类覆盖ThreadLocal的initialValue()方法…

1.简单介绍

在ThreadLocal类中有一个Map,用于存储每一个线程的变量副本,Map中元素的键为线程对象,而值对应线程的变量副本。
下面是示例:

public class ThreadLocalTest {/*1通过匿名内部类覆盖ThreadLocal的initialValue()方法,指定初始值  */private static ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>() {  public Integer initialValue() {  return 0;  }  };  /*2获取下一个序列值  */public int getNextNum() {  seqNum.set(seqNum.get() + 1);  return seqNum.get();  }  public static void main(String args[]){System.out.println("ThreadLocalTest start");ThreadLocalTest sn = new ThreadLocalTest();  /*3个线程共享sn,各自产生序列号  */TestClient t1 = new TestClient(sn);  TestClient t2 = new TestClient(sn);  TestClient t3 = new TestClient(sn);  t1.start();  t2.start();  t3.start(); }private static class TestClient extends Thread {  private ThreadLocalTest sn;  public TestClient(ThreadLocalTest sn) {  this.sn = sn;  }  public void run() {  for (int i = 0; i < 3; i++) {  /*4每个线程打出3个序列值  */System.out.println("thread[" + Thread.currentThread().getName() + "] --> sn["  + sn.getNextNum() + "]");  }  }  }  
}

使用例子:

    /**sdf有全局变量线程不安全,用ThreadLocal提供线程安全的sdf*/public static final ThreadLocal<DateFormat> SDF = new ThreadLocal<DateFormat>() {@Overrideprotected DateFormat initialValue() {return new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");}};

2.比较优秀的使用例子2(重要):

public class Port implements IThreadCase{//当前线程的Port实例private final static ThreadLocal<Port> portCurrent = new ThreadLocal<>();//线程管理类private final ThreadHandler thread;public Port(){this.thread = new ThreadHandler(this);}/** 开始 */public Port startup() {// 启动独立线程this.thread.setName(toString());this.thread.startup();return this;}/** 结束 */public void stop() {if (thread == null)return;// 停止独立线程this.thread.cleanup();}/** 获取当前线程的Port实例  */@SuppressWarnings("unchecked")public static <T extends Port> T getCurrent() {return (T) portCurrent.get();}@Overridepublic void caseStart() {portCurrent.set(this);}@Overridepublic void caseStop() {portCurrent.set(null);}
}

3. ThreadLocal会造成内存泄漏吗?

不同线程反复对ThreadLocal设置自己的值,线程会自己结束,但是没有对ThreadLocal存储本线程的内容做移除,会造成内存泄漏吗。
答案是不会的。
示例代码:

public class ThreadLocalTest {public static final int _10MB = 1024  * 1024 * 10;public static ThreadLocal<Object> threadLocal = new ThreadLocal(){@Overridepublic Object initialValue(){return null;}};public static void main(String args[]){System.out.println();for(;;){new Thread(){@Overridepublic void run() {byte[] data = new byte[_10MB];threadLocal.set(data);}}.start();ThreadTool.sleep(1000);System.out.println(Sys.getJVMStatus());}}
}

运行结果:
maxM=3641 MB, totalM=245.5 MB, freeM=231.66 MB, usedM=13.84 MB
maxM=3641 MB, totalM=245.5 MB, freeM=220.38 MB, usedM=25.12 MB
maxM=3641 MB, totalM=245.5 MB, freeM=209.1 MB, usedM=36.4 MB
maxM=3641 MB, totalM=245.5 MB, freeM=197.82 MB, usedM=47.68 MB
maxM=3641 MB, totalM=245.5 MB, freeM=186.54 MB, usedM=58.96 MB
maxM=3641 MB, totalM=245.5 MB, freeM=233.51 MB, usedM=11.99 MB
maxM=3641 MB, totalM=245.5 MB, freeM=222.22 MB, usedM=23.28 MB
maxM=3641 MB, totalM=245.5 MB, freeM=211.58 MB, usedM=33.92 MB
maxM=3641 MB, totalM=245.5 MB, freeM=200.94 MB, usedM=44.56 MB
maxM=3641 MB, totalM=245.5 MB, freeM=190.3 MB, usedM=55.2 MB
maxM=3641 MB, totalM=245.5 MB, freeM=232.87 MB, usedM=12.63 MB
maxM=3641 MB, totalM=245.5 MB, freeM=221.16 MB, usedM=24.34 MB
maxM=3641 MB, totalM=245.5 MB, freeM=209.88 MB, usedM=35.62 MB
maxM=3641 MB, totalM=245.5 MB, freeM=198.6 MB, usedM=46.9 MB
maxM=3641 MB, totalM=245.5 MB, freeM=187.32 MB, usedM=58.18 MB
maxM=3641 MB, totalM=245.5 MB, freeM=233.56 MB, usedM=11.94 MB
maxM=3641 MB, totalM=245.5 MB, freeM=222.65 MB, usedM=22.85 MB
maxM=3641 MB, totalM=245.5 MB, freeM=212.01 MB, usedM=33.49 MB
maxM=3641 MB, totalM=245.5 MB, freeM=201.37 MB, usedM=44.13 MB
maxM=3641 MB, totalM=245.5 MB, freeM=190.73 MB, usedM=54.77 MB
maxM=3641 MB, totalM=245.5 MB, freeM=234.2 MB, usedM=11.3 MB
maxM=3641 MB, totalM=245.5 MB, freeM=222.74 MB, usedM=22.76 MB
maxM=3641 MB, totalM=245.5 MB, freeM=211.46 MB, usedM=34.04 MB
maxM=3641 MB, totalM=245.5 MB, freeM=200.18 MB, usedM=45.32 MB
maxM=3641 MB, totalM=245.5 MB, freeM=188.9 MB, usedM=56.6 MB
maxM=3641 MB, totalM=234.5 MB, freeM=222.59 MB, usedM=11.91 MB
maxM=3641 MB, totalM=234.5 MB, freeM=211.23 MB, usedM=23.27 MB
原因分析:
我们看下get和Set

    public T get() {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null) {ThreadLocalMap.Entry e = map.getEntry(this);if (e != null) {@SuppressWarnings("unchecked")T result = (T)e.value;return result;}}return setInitialValue();}public void set(T value) {Thread t = Thread.currentThread();ThreadLocalMap map = getMap(t);if (map != null)map.set(this, value);elsecreateMap(t, value);}

ThreadLocal.ThreadLocalMap.Entry是啥?

  /*** ThreadLocalMap is a customized hash map suitable only for* maintaining thread local values. No operations are exported* outside of the ThreadLocal class. The class is package private to* allow declaration of fields in class Thread.  To help deal with* very large and long-lived usages, the hash table entries use* WeakReferences for keys. However, since reference queues are not* used, stale entries are guaranteed to be removed only when* the table starts running out of space.*/static class ThreadLocalMap {/*** The entries in this hash map extend WeakReference, using* its main ref field as the key (which is always a* ThreadLocal object).  Note that null keys (i.e. entry.get()* == null) mean that the key is no longer referenced, so the* entry can be expunged from table.  Such entries are referred to* as "stale entries" in the code that follows.*/static class Entry extends WeakReference<ThreadLocal<?>> {/** The value associated with this ThreadLocal. */Object value;Entry(ThreadLocal<?> k, Object v) {super(k);value = v;}}

因为继承了
WeakReference<?>所以是弱引用,每次GC的时候总会被回收

http://www.lbrq.cn/news/1454959.html

相关文章:

  • wordpress 学院 模板/在seo优化中
  • 手机网站建设 广州/百度搜索引擎的网址是多少
  • 个人博客网站制作图片/上海百度推广优化排名
  • 即墨有做网站的吗/东莞服务好的营销型网站建设
  • 青岛网站建设运营/青岛网站seo服务
  • 医疗网站建设咨询/网络推广需要多少费用
  • 网站设计实训心得/东莞网络科技公司排名
  • 网站开发公司取名/百度竞价推广开户联系方式
  • 国外设计师灵感网站/b2b免费发布网站大全
  • 求免费的那种网站有哪些/长尾关键词爱站
  • 注册了域名之后如何建立一个网站/中文域名的网站
  • wordpress上传大小/二级域名和一级域名优化难度
  • 手机管理网站模板/百度一下你就知道搜索
  • 外贸网站建设 杭州/官方网站怎么注册
  • 石家庄网站建设公司/汕头网站建设技术外包
  • 企业网站建设需要注意什么/网页设计与制作教程
  • 南昌市,做网站的公司/app推广方案模板
  • 网站淘宝客怎么做的/网站建设制作免费
  • 网站建设公司河南郑州/能搜任何网站的浏览器
  • 那个公司做的外贸网站好/it培训学校it培训机构
  • 房地产集团网站建设方案/张掖seo
  • 网站制作的/什么是口碑营销
  • 免费网站下载app软件/推广网站哪个好
  • 可以做微信游戏的网站有哪些/网络推广工作是做什么的
  • wordpress多站点功能/网课培训机构排名前十
  • 武汉专业网站做网页/推广自己的网站
  • 大连建设工程网站/清远新闻最新
  • 学校门户网站建设方案/百度网盘资源共享
  • 网站切换中英文/公司建网站多少钱
  • 如何做正规的采集网站/百度链接收录
  • Redis 常用数据结构以及单线程模型
  • 数据结构---配置网络步骤、单向链表额外应用
  • JS--获取事件的子元素与父元素
  • Nginx vs Spring Cloud Gateway:限流功能深度对比与实践指南
  • 【从零开始学习Redis】初识Redis
  • 力扣-437.路径总和III