safelistcopy.java
来自「Java Thread Programming (Source」· Java 代码 · 共 48 行
JAVA
48 行
import java.util.*;
public class SafeListCopy extends Object {
public static void printWords(String[] word) {
System.out.println("word.length=" + word.length);
for ( int i = 0; i < word.length; i++ ) {
System.out.println("word[" + i + "]=" + word[i]);
}
}
public static void main(String[] args) {
// To be safe, only keep a reference to the
// *synchronized* list so that you are sure that
// all accesses are controlled.
List wordList =
Collections.synchronizedList(new ArrayList());
wordList.add("Synchronization");
wordList.add("is");
wordList.add("important");
// First technique (favorite)
String[] wordA =
(String[]) wordList.toArray(new String[0]);
printWords(wordA);
// Second technique
String[] wordB;
synchronized ( wordList ) {
int size = wordList.size();
wordB = new String[size];
wordList.toArray(wordB);
}
printWords(wordB);
// Third technique (the 'synchronized' *is* necessary)
String[] wordC;
synchronized ( wordList ) {
wordC = (String[]) wordList.toArray(
new String[wordList.size()]);
}
printWords(wordC);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?