問題一覧に戻る
上級高度な機能
問題113: 並行コレクション
並行アクセスのためのスレッドセーフなコレクションを使用します。マルチスレッドアプリケーションで並列マップ操作のConcurrentHashMapとプロデューサー・コンシューマーパターンのBlockingQueueをマスターします。
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
// 並行マップ
<String, Integer> map = new <>();
map.put("A", 1);
map.("A", 2);
// ブロッキングキュー
<String> queue = new <>(3);
queue.("Item1");
String item = queue.();
}
}