Java 线程安全的LinkedList替代使用

Java 线程安全的LinkedList替代使用,java,multithreading,thread-safety,Java,Multithreading,Thread Safety,目前我正在使用LinkedList添加所有命令信息。如何使下面的列表线程安全?我是否应该在这里使用其他选项而不是LinkedList private static List<Command> commands; Command command = new Command(); command.setName(commandName); command.setExecutionPercentage(Double.parseDouble(percentage)); command.se

目前我正在使用
LinkedList
添加所有
命令
信息。如何使下面的
列表
线程安全?我是否应该在这里使用其他选项而不是
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);
如果我做下面这样的事情呢-

Collections.synchronizedList(commands.add(command));
或者我需要这样做-

commands = Collections.synchronizedList(new LinkedList<Command>());
我收到错误-

 The method get(int) is undefined for the type Queue<Command>
类型队列的方法get(int)未定义
如何使下面的列表线程安全?这里有没有其他选项可以代替LinkedList

是一个并发链接队列。引用javadocs:

基于链接节点的无限线程安全队列。该队列对元素进行FIFO排序(先进先出)。队列的头是在队列上停留时间最长的元素。队列的尾部是在队列上停留时间最短的元素。新元素插入到队列的尾部,队列检索操作获取队列头部的元素。当许多线程将共享对公共集合的访问时,ConcurrentLinkedQueue是一个合适的选择。此队列不允许空元素

所以你会做一些类似的事情:

 Queue<Command> concurrentQueue = new ConcurrentLinkedQueue<Command>();
 Command command = new Command();
 ...
 concurrentQueue.add(command);
如果您提供有关如何使用此队列的更多信息,我们可以提供更多关于正确的JDK并发集合的替代方案

Collections.synchronizedList(commands.add(command))

您编辑了问题并询问了上述代码。它不会编译,因为
List.add(…)
返回一个布尔值

如何使下面的列表线程安全?这里有没有其他选项可以代替LinkedList

是一个并发链接队列。引用javadocs:

基于链接节点的无限线程安全队列。该队列对元素进行FIFO排序(先进先出)。队列的头是在队列上停留时间最长的元素。队列的尾部是在队列上停留时间最短的元素。新元素插入到队列的尾部,队列检索操作获取队列头部的元素。当许多线程将共享对公共集合的访问时,ConcurrentLinkedQueue是一个合适的选择。此队列不允许空元素

所以你会做一些类似的事情:

 Queue<Command> concurrentQueue = new ConcurrentLinkedQueue<Command>();
 Command command = new Command();
 ...
 concurrentQueue.add(command);
如果您提供有关如何使用此队列的更多信息,我们可以提供更多关于正确的JDK并发集合的替代方案

Collections.synchronizedList(commands.add(command))


您编辑了问题并询问了上述代码。它不会编译,因为
列表。add(…)
返回一个布尔值。

我更新了我的问题,提供了更多详细信息,你能在我的示例基础上为我展示一个
ConncurrentLinkedQueue
的示例吗。嗯,ok@Nevzz03。完成。这很琐碎。谢谢格雷的建议。更新了我的问题,因为我正在使用
ConncurrentLinkedQueue
时更改了代码,但在
get
调用时出错。为什么?如果我需要使用
LinkedList
,那么这也可以,对吗
commands=Collections.synchronizedList(新的LinkedList())编辑你的问题并在那里提出一个全新的问题@Nevzz03是不对的<代码>队列
不是
列表
。没有像上面所说的那样的
get(int)
方法。您可能必须使用集合。synchronizedList(…)。我更新了我的问题,提供了更多详细信息,您能否在我的示例基础上为我展示一个
ConncurrentLinkedQueue
示例。嗯,ok@Nevzz03。完成。这很琐碎。谢谢格雷的建议。更新了我的问题,因为我正在使用
ConncurrentLinkedQueue
时更改了代码,但在
get
调用时出错。为什么?如果我需要使用
LinkedList
,那么这也可以,对吗
commands=Collections.synchronizedList(新的LinkedList())编辑你的问题并在那里提出一个全新的问题@Nevzz03是不对的<代码>队列
不是
列表
。没有像上面所说的那样的
get(int)
方法。您可能必须使用
Collections.synchronizedList(…)
。我的答案显示了
Collections.synchronizedList(…)
的正确使用。我的答案显示了
Collections.synchronizedList(…)
的正确使用。
 The method get(int) is undefined for the type Queue<Command>
 Queue<Command> concurrentQueue = new ConcurrentLinkedQueue<Command>();
 Command command = new Command();
 ...
 concurrentQueue.add(command);
// turn the commands list into a synchronized list by wrapping it
commands = Collections.synchronizedList(commands);