线程安全LinkedList的替代使用 [英] Thread Safe LinkedList alternate use

查看:503
本文介绍了线程安全LinkedList的替代使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用LinkedList添加所有Command信息.如何使以下List<Command>线程安全?还有什么我应该在这里使用的其他选项,而不是LinkedList?

Currently I am using LinkedList to add all the Command information. How can I make the below List<Command> thread safe? Is there any other option I should be using here instead of 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中.

I am adding all the above command that I am getting by reading from the text file and putting it into the LinkedList of command just like below. So suppose if I have three command then I need to add all those three command in to some LinkedList which is what I was doing.

commands.add(command);

如果我做下面的事情怎么办?-

What If I do something like below?-

Collections.synchronizedList(commands.add(command));

或者我需要做这样的事情-

or I need to do something like this-

commands = Collections.synchronizedList(new LinkedList<Command>());

更新:-

根据您的建议,如果我正在使用-

As per your suggestion, If I am using -

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

And after sometime basically when all the initialization has been done, I need to get the command information from the Queue, So I was doing something like this previously with the use of LinkedList. But after chaning to ConcurrentLinkedQueue, this get call is giving me an error as there is a error line on get call

commands.get(commandWithMaxNegativeOffset);

我遇到的错误-

 The method get(int) is undefined for the type Queue<Command>

推荐答案

如何使以下列表线程安全?还有其他我应该在这里使用的替代LinkedList的选项吗?

How can I make the below List thread safe? Is there any other option I should be using here instead of LinkedList?

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

ConcurrentLinkedQueue is a concurrent linked queue. To quote from the javadocs:

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

An unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. This queue does not permit null elements.

因此,您将执行以下操作:

So you'd do something like:

 Queue<Command> concurrentQueue = new ConcurrentLinkedQueue<Command>();
 Command command = new Command();
 ...
 concurrentQueue.add(command);

您还可以使用Collections.synchronizedList(...)包装当前的List.是否执行此操作或使用ConcurrentLinkedQueue取决于收集需要达到的高性能.

You can also wrap your current List using the Collections.synchronizedList(...). Whether to do this or use ConcurrentLinkedQueue depends on how high performance you need the collection to be.

// turn the commands list into a synchronized list by wrapping it
commands = Collections.synchronizedList(commands);

如果您提供有关如何使用此队列的更多信息,那么我们可以在适当的JDK并发集合方面提供更多选择.

If you provide some more information about how you are using this queue, we can provide possibly more alternatives in terms of the proper JDK concurrent collection.

Collections.synchronizedList(commands.add(command));

Collections.synchronizedList(commands.add(command));

您编辑了问题,并询问了上述代码.由于List.add(...)返回一个布尔值,它将无法编译.

You edited your question and asked about the above code. It won't compile since List.add(...) returns a boolean.

这篇关于线程安全LinkedList的替代使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆