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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:37:34+00:00 2026-05-18T06:37:34+00:00

I hope in a good manner :-) I wrote this piece of code. What

  • 0

I hope in a good manner 🙂
I wrote this piece of code.
What I wished to do, is to build something like “cache”.
I assumed that I had to watch for different threads, as might many calls get to that class, so I tried the ThreadLocal functionality.
Base pattern is
have “MANY SETS of VECTOR”
The vector holds something like:
VECTOR.FieldName = “X”
VECTOR.FieldValue= “Y”
So many Vector objects in a set. Different set for different calls from different machines, users, objects.

 private static CacheVector instance = null;
        private static SortedSet<SplittingVector> s = null;
        private static TreeSet<SplittingVector> t = null;
        private static ThreadLocal<SortedSet<SplittingVector>> setOfVectors = new ThreadLocal<SortedSet<SplittingVector>>();

        private static class MyComparator implements Comparator<SplittingVector> {
     public int compare(SplittingVector a, SplittingVector b) {
         return 1;
     }
     // No need to override equals.
        }

        private CacheVector() {
        }

        public static SortedSet<SplittingVector> getInstance(SplittingVector vector) {
     if (instance == null) {
         instance = new CacheVector();
         //TreeSet<SplittingVector>
         t = new TreeSet<SplittingVector>(new MyComparator());
         t.add(vector);
         s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
         CacheVector.assign(s);
     } else {
         //TreeSet<SplittingVector> t = new TreeSet<SplittingVector>();
         t.add(vector);
         s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
         CacheVector.assign(s);
     }
     return CacheVector.setOfVectors.get();
        }

        public SortedSet<SplittingVector> retrieve() throws Exception {
     SortedSet<SplittingVector> set = setOfVectors.get();
     if (set == null) {
         throw new Exception("SET IS EMPTY");
     }
     return set;
        }

        private static void assign(SortedSet<SplittingVector> nSet) {
     CacheVector.setOfVectors.set(nSet);
        }

So… I have it in the attach and I use it like this:

CachedVector cache = CachedVector.getInstance(bufferedline);

The nice part: Bufferedline is a splitted line based on some delimiter from data files. Files can be of any size.

So how do you see this code? Should I be worry ?
I apologise for the size of this message!

  • 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-05-18T06:37:34+00:00Added an answer on May 18, 2026 at 6:37 am

    Writing correct multi-threaded code is not that easy (i.e. your singleton fails to be), so try to rely on existing solutions if posssible. If you’re searching for a thread-safe Cache implementation in Java, check out this LinkedHashMap. You can use it to implement a LRU cache. And collections.synchronizedMap(). can make this thread-safe.

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

Sidebar

Related Questions

I hope with this question that we can assemble a bunch of good advices
Hope somebody has a good idea. I have strings like this: abcdefg abcde abc
Hope this is not a dupe. I would like to be able to do
I'm not very good in SQL stuff, so I hope someone can explain this
Ok, there is a good chance I'm not explaining this correctly but I hope
Hope you all had a good weekend. We're just about to sign up to
I hope that in this post, I can get people's opinions on best practices
Good morning everyone, I hope I'm not posting this in the wrong place; I've
Hey folks, hope you've all had a good break over the holidays. I've created
this is my first question here. I hope dont make any mistake, be good

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.