超碰免费人人操|国产视频二区久久艹人人操|欧美激情第一页在线|久热最新无码中文视频|91精品国际成人|亚洲成人精品在线视频青青草|久草免费高清完整在线观看|你懂的AV在线日本黄网页|国产黄色AV日韩女同网|欧美成人色区导航片av

<dl id="viuws"></dl>
    <strike id="viuws"><video id="viuws"></video></strike>
  1. <mark id="viuws"></mark>

    Java并發(fā)編程:深入剖析ThreadLocal

    時(shí)間:2025-11-20 22:05:13 java語(yǔ)言

    Java并發(fā)編程:深入剖析ThreadLocal

      ThreadLocal類可以理解為ThreadLocalVariable(線程局部變量),提供了get與set等訪問(wèn)接口或方法,這些方法為每個(gè)使用該變量的線程都存有一份獨(dú)立的副本,因此get總是返回當(dāng)前執(zhí)行線程在調(diào)用set時(shí)設(shè)置的最新值?梢詫hreadLocal視為 包含了Map對(duì)象,保存了特定于該線程的值。

      概括起來(lái)說(shuō),對(duì)于多線程資源共享的問(wèn)題,同步機(jī)制采用了“以時(shí)間換空間”的方式,而ThreadLocal采用了“以空間換時(shí)間”的方式。前者僅提供一份變量,讓不同的線程排隊(duì)訪問(wèn),而后者為每一個(gè)線程都提供了一份變量,因此可以同時(shí)訪問(wèn)而互不影響。

      模擬ThreadLocal

      復(fù)制代碼 代碼如下:

      import java.util.Collections;

      import java.util.HashMap;

      import java.util.Map;

      public class SimpleThreadLocal{

      private MapvalueMap = Collections

      .synchronizedMap(new HashMap());

      public void set(T newValue) {

      valueMap.put(Thread.currentThread(), newValue); /pic/p>

      }

      public T get() {

      Thread currentThread = Thread.currentThread();

      T o = valueMap.get(currentThread); /pic/p>

      if (o == null && !valueMap.containsKey(currentThread)) { /pic/p>

      o = initialValue();

      valueMap.put(currentThread, o);

      }

      return o;

      }

      public void remove() {

      valueMap.remove(Thread.currentThread());

      }

      protected T initialValue() {

      return null;

      }

      }

      實(shí)用ThreadLocal

      復(fù)制代碼 代碼如下:

      class Count {

      private SimpleThreadLocalcount = new SimpleThreadLocal() {

      @Override

      protected Integer initialValue() {

      return 0;

      }

      };

      public Integer increase() {

      count.set(count.get() + 1);

      return count.get();

      }

      }

      class TestThread implements Runnable {

      private Count count;

      public TestThread(Count count) {

      this.count = count;

      }

      @Override

      public void run() {

      /pic/p>

      for (int i = 1; i <= 3; i++) {

      System.out.println(Thread.currentThread().getName() + "t" + i

      + "tht" + count.increase());

      }

      }

      }

      public class TestThreadLocal {

      public static void main(String[] args) {

      Count count = new Count();

      Thread t1 = new Thread(new TestThread(count));

      Thread t2 = new Thread(new TestThread(count));

      Thread t3 = new Thread(new TestThread(count));

      Thread t4 = new Thread(new TestThread(count));

      t1.start();

      t2.start();

      t3.start();

      t4.start();

      }

      }

      輸出

      復(fù)制代碼 代碼如下:

      Thread-0 1th 1

      Thread-0 2th 2

      Thread-0 3th 3

      Thread-3 1th 1

      Thread-1 1th 1

      Thread-1 2th 2

      Thread-2 1th 1

      Thread-1 3th 3

      Thread-3 2th 2

      Thread-3 3th 3

      Thread-2 2th 2

      Thread-2 3th 3

    【Java并發(fā)編程:深入剖析ThreadLocal】相關(guān)文章:

    java并發(fā)編程參考12-01

    簡(jiǎn)單地分析Java線程編程中ThreadLocal類的使用01-29

    java編程術(shù)語(yǔ)03-09

    Java編程語(yǔ)言02-10

    java編程基礎(chǔ)12-13

    java教程之Java編程基礎(chǔ)12-06

    Java中同步與并發(fā)的運(yùn)用02-06

    java面向接口編程02-19

    Java語(yǔ)言的編程特點(diǎn)12-08