⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 safelistcopy.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -