From 906e58ef7cad4f0fcc08c41e7223c942679ce69d Mon Sep 17 00:00:00 2001 From: xiangflight Date: Thu, 12 Jul 2018 14:51:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86CopyOnWriteArrayList=E7=9A=84=E7=AE=80=E4=BB=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes notes/Java 容器.md | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b36190334234c2eda81663fa9356d3044da733a1 GIT binary patch literal 6148 zcmeHK&1(}u6n|5r?lyjGYpbm&EaqUrB9&G>h`7d9Eh?!=tc}&iO?H|tZfC;ornQzr z5WEUr6~Tiac=8VrJg5i%1TW&BpcfCG^vy?TO!X=v`@+oc<<0NC-T5uEvjYGm+whJ6 zSO8#P6PW78?l&gJMcZIqIwA&%tZ@-0p#ViV42v7sNgz=mQQ%)wK&;&~WFd#Y3(&rP zy~%)AD*>+{L%beNU>o`&4LyL(z<@&x6rv91%Re(K%TZm|{8p(4eiV<4d?a0)Qk%DI zHO%g`nck5$Zxj~zav^r(MwE5KvbC?^d$i()SA8#DC{i{N&?U-p;YBy5;jADRIS-Ja ze77>~)8(?&cZRdr^%)IiJyqlSRiAl$Im;W&izHtcm;RI+FJK)b$qOtqt3&$B&N;+he20*49kJ7(943Ki9Z>|KX#j&z`?{`%Y#7!PlX*9*r${ zk4~F#esxL9LEg0F_-t8if^6HqW2e>AyQ`=TMx)`=R!)Ete2agk7zto zwsuC;EoVqM7G#tIka1QV=^GX)GR~-J zW}GQKYQP~l4VPdVZowUR0FU7nyoL|(3BJI0vXu;y1LP1HBPYoza-Li!c~T@La*fQY z{I1q>-LLCuARanx&vmH~vVgN1Mny&n>fl4QbD@wuIM%+TOvu9o-rqRh;e^~oI~F0i z*OAeU+wB!(U&5?Blzx{Z7w+@6g^MMW>B$)BRv^Jdfkc5s0j+>|KiJp=IvSe_<*Nfb zc?3XAVOSda;$uPN*cu&;O@%muMpP=IN+tS;K~y@%ZOwBuHWjLLAo}n@^p%M|p%8g> ztZz#?5JzFsCkiAA{Hnkv_16;n|HPlq|Gy?B{}Kff1^z1qNNT(=p2aEYy>-Ln#9nJ- uyNOMh;58M>6zueNEFG~G4`7pqW43S*9gR(e7(q)u1f&c}mniU875E7(>Kowz literal 0 HcmV?d00001 diff --git a/notes/Java 容器.md b/notes/Java 容器.md index 55cffe2c..bdb97bb5 100644 --- a/notes/Java 容器.md +++ b/notes/Java 容器.md @@ -255,6 +255,31 @@ List synList = Collections.synchronizedList(list); List list = new CopyOnWriteArrayList<>(); ``` +扩展:CopyOnWriteArrayList是一种CopyOnWrite容器,即写时复制的容器。从以下源码中看出:当向容器中添加元素时,不是直接往当前容器添加,而是Copy当前容器,在新容器中添加,添加完后,将原容器的引用指向新容器。这样做可以对CopyOnWrite容器进行并发的读,不需要加锁,极大地提高了读性能,因此适用于读多写少的场景。 + +```java +public boolean add(E e) { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + Object[] elements = getArray(); + int len = elements.length; + Object[] newElements = Arrays.copyOf(elements, len + 1); // 复制到新容器 + newElements[len] = e; + setArray(newElements); + return true; + } finally { + lock.unlock(); + } +} + +@SuppressWarnings("unchecked") +private E get(Object[] a, int index) { + return (E) a[index]; +} + +``` + ## LinkedList ### 1. 概览 From 08457794f7771a6417991d614975d26a378a9f5a Mon Sep 17 00:00:00 2001 From: xiangflight Date: Thu, 12 Jul 2018 14:54:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0DS=5Fstore=E5=88=B0git.ig?= =?UTF-8?q?nore=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 1 + 2 files changed, 1 insertion(+) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index b36190334234c2eda81663fa9356d3044da733a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK&1(}u6n|5r?lyjGYpbm&EaqUrB9&G>h`7d9Eh?!=tc}&iO?H|tZfC;ornQzr z5WEUr6~Tiac=8VrJg5i%1TW&BpcfCG^vy?TO!X=v`@+oc<<0NC-T5uEvjYGm+whJ6 zSO8#P6PW78?l&gJMcZIqIwA&%tZ@-0p#ViV42v7sNgz=mQQ%)wK&;&~WFd#Y3(&rP zy~%)AD*>+{L%beNU>o`&4LyL(z<@&x6rv91%Re(K%TZm|{8p(4eiV<4d?a0)Qk%DI zHO%g`nck5$Zxj~zav^r(MwE5KvbC?^d$i()SA8#DC{i{N&?U-p;YBy5;jADRIS-Ja ze77>~)8(?&cZRdr^%)IiJyqlSRiAl$Im;W&izHtcm;RI+FJK)b$qOtqt3&$B&N;+he20*49kJ7(943Ki9Z>|KX#j&z`?{`%Y#7!PlX*9*r${ zk4~F#esxL9LEg0F_-t8if^6HqW2e>AyQ`=TMx)`=R!)Ete2agk7zto zwsuC;EoVqM7G#tIka1QV=^GX)GR~-J zW}GQKYQP~l4VPdVZowUR0FU7nyoL|(3BJI0vXu;y1LP1HBPYoza-Li!c~T@La*fQY z{I1q>-LLCuARanx&vmH~vVgN1Mny&n>fl4QbD@wuIM%+TOvu9o-rqRh;e^~oI~F0i z*OAeU+wB!(U&5?Blzx{Z7w+@6g^MMW>B$)BRv^Jdfkc5s0j+>|KiJp=IvSe_<*Nfb zc?3XAVOSda;$uPN*cu&;O@%muMpP=IN+tS;K~y@%ZOwBuHWjLLAo}n@^p%M|p%8g> ztZz#?5JzFsCkiAA{Hnkv_16;n|HPlq|Gy?B{}Kff1^z1qNNT(=p2aEYy>-Ln#9nJ- uyNOMh;58M>6zueNEFG~G4`7pqW43S*9gR(e7(q)u1f&c}mniU875E7(>Kowz diff --git a/.gitignore b/.gitignore index e69de29b..e43b0f98 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store From e7d694d841049c93dc9dce27e6846231a56fb32f Mon Sep 17 00:00:00 2001 From: xiangflight Date: Thu, 12 Jul 2018 15:15:06 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9CopyOnWrite=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=9A=84=E8=A1=A8=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/Java 容器.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/notes/Java 容器.md b/notes/Java 容器.md index bdb97bb5..cc4e93ad 100644 --- a/notes/Java 容器.md +++ b/notes/Java 容器.md @@ -255,17 +255,18 @@ List synList = Collections.synchronizedList(list); List list = new CopyOnWriteArrayList<>(); ``` -扩展:CopyOnWriteArrayList是一种CopyOnWrite容器,即写时复制的容器。从以下源码中看出:当向容器中添加元素时,不是直接往当前容器添加,而是Copy当前容器,在新容器中添加,添加完后,将原容器的引用指向新容器。这样做可以对CopyOnWrite容器进行并发的读,不需要加锁,极大地提高了读性能,因此适用于读多写少的场景。 +扩展 +CopyOnWriteArrayList是一种CopyOnWrite容器,从以下源码看出:添加元素是在复制的新数组上进行的,然后将原数组的引用指向新数组;读取元素是从原数组读取。这样可以进行并发的且不需加锁的读取,读取效率高,适用于读操作远大于写操作的场景中。 ```java public boolean add(E e) { - final ReentrantLock lock = this.lock; + final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; - Object[] newElements = Arrays.copyOf(elements, len + 1); // 复制到新容器 - newElements[len] = e; + Object[] newElements = Arrays.copyOf(elements, len + 1); + newElements[len] = e; setArray(newElements); return true; } finally { @@ -273,9 +274,13 @@ public boolean add(E e) { } } +final void setArray(Object[] a) { + array = a; +} + @SuppressWarnings("unchecked") private E get(Object[] a, int index) { - return (E) a[index]; + return (E) a[index]; } ``` From 1e26cd1af42cead6aa6e6494158ef0373920533c Mon Sep 17 00:00:00 2001 From: xiangflight Date: Thu, 12 Jul 2018 19:45:30 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AE=80=E5=8C=96=E5=AF=B9CopyOnWrite?= =?UTF-8?q?=E7=9A=84=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/Java 容器.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/notes/Java 容器.md b/notes/Java 容器.md index cc4e93ad..f5e7ee10 100644 --- a/notes/Java 容器.md +++ b/notes/Java 容器.md @@ -255,8 +255,7 @@ List synList = Collections.synchronizedList(list); List list = new CopyOnWriteArrayList<>(); ``` -扩展 -CopyOnWriteArrayList是一种CopyOnWrite容器,从以下源码看出:添加元素是在复制的新数组上进行的,然后将原数组的引用指向新数组;读取元素是从原数组读取。这样可以进行并发的且不需加锁的读取,读取效率高,适用于读操作远大于写操作的场景中。 +CopyOnWriteArrayList是一种CopyOnWrite容器,从以下源码看出:读取元素是从原数组读取;添加元素是在复制的新数组上。读写分离,因而可以在并发条件下进行不加锁的读取,读取效率高,适用于读操作远大于写操作的场景。 ```java public boolean add(E e) {