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

📄 safecollectioniteration.java

📁 Java Thread Programming (Source
💻 JAVA
字号:
import java.util.*;

public class SafeCollectionIteration extends Object {
	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.

		// The collection *must* be synchronized 
		// (a List in this case).
		List wordList = 
				Collections.synchronizedList(new ArrayList());

		wordList.add("Iterators");
		wordList.add("require");
		wordList.add("special");
		wordList.add("handling");

		// All of this must be in a synchronized block to 
		// block other threads from modifying wordList while 
		// the iteration is in progress.
		synchronized ( wordList ) {
			Iterator iter = wordList.iterator();
			while ( iter.hasNext() ) {
				String s = (String) iter.next();
				System.out.println("found string: " + s + 
					", length=" + s.length());
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -