Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:12:57+00:00 2026-06-10T03:12:57+00:00

Currently I am using LinkedList to add all the Command information. How can I

  • 0

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);

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>());

Update:-

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);

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);

Error I am getting-

 The method get(int) is undefined for the type Queue<Command>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:12:58+00:00Added an answer on June 10, 2026 at 3:12 am

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

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

    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);
    

    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);
    

    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));

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

heres what I'm currently using: Rails -v 3.2.3 I'm trying to make a path
Is there a pre-implemented linked list out there that would have been implemented using
Currently in a multithreaded environment, we are using a LinkedList to hold data. Sometimes
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using MySQL version 5.1.6 This is my first real world build and so
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I
I currently using android NDK to write some native code in C. I have
I'm currently using the NEST ElasticSearch C# Library for interacting with ElasticSearch. My project
I'm currently using inline javascript to clear an input text's default value on focus.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.