GeezerVison is showing github.com — Merge pull request #1211 from drlifeL/master · zjtech/JavaGuide@cbcf184
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: docs/java/collection/ArrayList源码+扩容机制分析.md +33Lines changed: 33 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -733,6 +733,24 @@ public class ArrayList<E> extends AbstractList<E> |
733 | 733 |
|
734 | 734 | #### 3.3.1. `System.arraycopy()` 方法 |
735 | 735 |
|
| 736 | +源码: |
| 737 | + |
| 738 | +```java |
| 739 | + // 我们发现 arraycopy 是一个 native 方法,接下来我们解释一下各个参数的具体意义 |
| 740 | + /** |
| 741 | + * 复制数组 |
| 742 | + * @param src 源数组 |
| 743 | + * @param srcPos 源数组中的起始位置 |
| 744 | + * @param dest 目标数组 |
| 745 | + * @param destPos 目标数组中的起始位置 |
| 746 | + * @param length 要复制的数组元素的数量 |
| 747 | + */ |
| 748 | + public static native void arraycopy(Object src, int srcPos, |
| 749 | + Object dest, int destPos, |
| 750 | + int length); |
| 751 | +``` |
| 752 | + |
| 753 | +场景: |
736 | 754 | ```java |
737 | 755 | /** |
738 | 756 | * 在此列表中的指定位置插入指定的元素。 |
@@ -781,6 +799,21 @@ public class ArraycopyTest { |
781 | 799 |
|
782 | 800 | #### 3.3.2. `Arrays.copyOf()`方法 |
783 | 801 |
|
| 802 | +源码: |
| 803 | + |
| 804 | +```java |
| 805 | + public static int[] copyOf(int[] original, int newLength) { |
| 806 | + // 申请一个新的数组 |
| 807 | + int[] copy = new int[newLength]; |
| 808 | + // 调用System.arraycopy,将源数组中的数据进行拷贝,并返回新的数组 |
| 809 | + System.arraycopy(original, 0, copy, 0, |
| 810 | + Math.min(original.length, newLength)); |
| 811 | + return copy; |
| 812 | + } |
| 813 | +``` |
| 814 | + |
| 815 | +场景: |
| 816 | + |
784 | 817 | ```java |
785 | 818 | /** |
786 | 819 | 以正确的顺序返回一个包含此列表中所有元素的数组(从第一个到最后一个元素); 返回的数组的运行时类型是指定数组的运行时类型。 |
|
Collapse file
Copy file name to clipboardExpand all lines: docs/java/multi-thread/2020最新Java并发进阶常见面试题总结.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -907,7 +907,7 @@ AtomicInteger 类主要利用 CAS (compare and swap) + volatile 和 native 方 |
907 | 907 |
|
908 | 908 | CAS 的原理是拿期望的值和原本的一个值作比较,如果相同则更新成新的值。UnSafe 类的 objectFieldOffset() 方法是一个本地方法,这个方法是用来拿到“原来的值”的内存地址,返回值是 valueOffset。另外 value 是一个 volatile 变量,在内存中可见,因此 JVM 可以保证任何时刻任何线程总能拿到该变量的最新值。 |
909 | 909 |
|
910 | | -关于 Atomic 原子类这部分更多内容可以查看我的这篇文章:并发编程面试必备:[JUC 中的 Atomic 原子类总结](https://mp.weixin.qq.com/s/joa-yOiTrYF67bElj8xqvg) |
| 910 | +关于 Atomic 原子类这部分更多内容可以查看我的这篇文章:并发编程面试必备:[JUC 中的 Atomic 原子类总结](https://mp.weixin.qq.com/s?__biz=Mzg2OTA0Njk0OA==&mid=2247484834&idx=1&sn=7d3835091af8125c13fc6db765f4c5bd&source=41#wechat_redirect) |
911 | 911 |
|
912 | 912 | ## 6. AQS |
913 | 913 |
|
|