現在、すべての情報LinkedList
を追加するために使用しています。以下のスレッドセーフCommand
にするにはどうすればよいですか?List<Command>
の代わりにここで使用する必要がある他のオプションはありますLinkedList
か?
private static List<Command> commands;
Command command = new Command();
command.setName(commandName);
command.setExecutionPercentage(Double.parseDouble(percentage));
command.setAttributeIDGet(attributeIDGet);
command.setAttributeIDSet(attributeIDSet);
command.setDataUsageCriteria(dataCriteria);
command.setUserLoggingCriteria(userLoggingCriteria);
command.setSleepTime(sleepTimeOfCommand);
command
テキストファイルから読み取って、LinkedList of command
以下のように配置することで取得した上記のすべてを追加しています。もし私が持っているなら、私がやっていたいくつかにthree command
それらすべてを追加する必要があるとします。three command
LinkedList
commands.add(command);
以下のようなことをするとどうなりますか?-
Collections.synchronizedList(commands.add(command));
または私はこのようなことをする必要があります-
commands = Collections.synchronizedList(new LinkedList<Command>());
アップデート:-
あなたの提案によると、私が使用している場合-
private static Queue<Command> commands;
commands = new ConcurrentLinkedQueue<Command>(); // Using linked list to ensure iteration order
Command command = new Command();
command.setName(commandName);
command.setExecutionPercentage(Double.parseDouble(percentage));
command.setAttributeIDGet(attributeIDGet);
command.setAttributeIDSet(attributeIDSet);
command.setDataUsageCriteria(dataCriteria);
command.setUserLoggingCriteria(userLoggingCriteria);
command.setSleepTime(sleepTimeOfCommand);
commands.add(command);
そして、基本的にすべての初期化が完了した後command information
、キューから を取得する必要があるため、以前は を使用してこのようなことをしていましたLinkedList
。しかし、 に変更した後ConcurrentLinkedQueue
、これget call is giving me an error
があるのでerror line on get call
commands.get(commandWithMaxNegativeOffset);
私が得ているエラー-
The method get(int) is undefined for the type Queue<Command>