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

  • Home
  • SEARCH
  • 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 8291977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:18:42+00:00 2026-06-08T13:18:42+00:00

I have a Java code which searches for values in the hash table in

  • 0

I have a Java code which searches for values in the hash table in following way:

class HTDemo {
    public static void main(String args[]) {
        Hashtable balance = new Hashtable();
        double bal;
        balance.put("John Doe", new Double(3434.34));
        balance.put("Tom Smith", new Double(123.22));
        balance.put("Jane Baker", new Double(1378.00));
        balance.put("Todd Hall", new Double(99.22));
        balance.put("Ralph Smith", new Double(-19.08));

        **System.out.println("John Doe's balance: " + balance.get("John Doe"));**
        **System.out.println("Tom Smith's balance: " + balance.get("Tom Smith"));**
        **System.out.println("Jane Baker's balance: " + balance.get("Jane Baker"));**
    }
 }

Now, I want to run it on multiple thread i.e. I want to modify the code in such a way thus the get methods (inside **) works concurrently. Can anybody help me how to do that. Actually, I am facing issue with passing hash tables and making it concurrent while running.

  • 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-08T13:18:43+00:00Added an answer on June 8, 2026 at 1:18 pm

    Hashtable is synchronized and therefore already thread safe. You don’t need anything else to access your code safely in a multi threaded environment.

    However, HashTable is obsolete and its performance will be quite poor in a highly concurrent environment due to the thread-safety mechanism that is implemented (all methods are synchronized) and the provided iterators are not thread safe and will fail if you modify the table while iterating.

    Bottom line: use a ConcurrentHashMap, and use generics if you can:

    Map<String, Double> balance = new ConcurrentHashMap<String, Double> ();
    

    The rest of your code should remain unchanged.

    One thing you also need to keep in mind, is that even with a thread safe data structue (be it a HashTable or a ConcurrentMap), you still need to deal with concurrency issues such as atiomcity.

    For example, if you check the balance to authorise a payment like this:

    double johnBalance = balance.get("John");
    if(johnBalance > paymentAmount) 
        authorisePayment();
    else
        declinePayment();
    

    There is an atomicity issue: the balance on John’s account might have changed between the call to balance.get(...) and the payment authorization.

    If this is one of your use cases, you will need to introduce an additional layer of synchronization.

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

Sidebar

Related Questions

I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int
I have the following code which searches a text file on my phone's sd
I have java code which connect to MySQL and retrive data. I have following
In my source tree have Java code which is automatically generated from XSD files.
I have a java code which gets the function names, their return types, their
I have some Java code which performs bitwise operations on a BitSet. I have
I have some Java code which looks roughly like this: String urlString = ftp://polar.ncep.noaa.gov/pub/history/waves/multi_1.glo_30m.dp.200601.grb2;
I have a Java handler code which Im using in android to run a
In my Java code I have embedded a SQL query which fetches data from
I have some code which I think should compile, but doesn't: import java.io.InputStream; import

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.