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 8017293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:48:16+00:00 2026-06-04T20:48:16+00:00

I have a private LinkedHashMap which is used for reading (only for reading) from

  • 0

I have a private LinkedHashMap which is used for reading (only for reading) from different threads:

class foo {

  private LinkedHashMap map = ...;


  public publicMethodCalledFromDifferentThreads() {

     for (Object foo : map) {
        ...
     }
  }
}

This implementation is not thread safe and so the for loop can’t behave correctly. I tried to find a solution by myself and the only thing I came up with is the following:

class foo {

  private LinkedHashMap map = ...;
  private Map.Entry[] mapEntries = map.entrySet().toArray() ...;

  public publicMethodCalledFromDifferentThreads() {

     for (int i = 0; i < mapEntries.length; i++) {
        mapEntries[i]...
        ...
     }
  }
}

So in the end I need to have two variables and the iteration gets really complex. What would be the nicest way to do this?

Thanks!

  • 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-04T20:48:18+00:00Added an answer on June 4, 2026 at 8:48 pm

    If you are only reading, and the underlying objects are not being modified, both the list and the things in the list, there should be no threading issue.

    What issue are you actually experiencing?

    I think it’s possible you are modifying your LinkedHashMap somehow.

    More over, how are you determining the threads are jumping around? Are you sure you are not just seeing output from the 2 threads intermixed, which would look like jumping around, even though each thread is iterating sequentially? For example if each thread printed its current entry you might see something like

    123 12 45 345 6….

    which is sequential….

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

Sidebar

Related Questions

In a class, I have: private Foo bar; public Constructor(Foo bar) { this.bar =
I have a public class FriendMaps extends MapActivity that gets called from a menu
I have a singleton class, that has a map which can be accessed by
I have private and public keys from the .Net system in the xml format.
I'm writing my own PriorityQueue class and I have: private Queue<E>[] queues; public PriorityQueue(int
I have a private class variable that holds a collection of order items: Private
I have a private method def __pickSide(self): in a parent class that I would
I have a private variable in my class: BOOL isEnabled; and a method: -(void)
Right now I have private static void iterateall(BinaryTree foo) { if(foo!= null){ System.out.println(foo.node); iterateall(foo.left);
Right now I have private static void getMethods(Class<? extends Object> clazz) { Method[] declaredMethods

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.