-1

I want to initialize a hashtable inside my class only once for my web application.

How can you do this in a thread-safe manner in case?

public class MySettings {

   private HashTable<int, SomeObject> settings;

}

Should it be marked static or final or volatile? Do I wrap it in a synchronize?

Also, how can I set the properties of SomeObject inline? Is it possible in java?

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • You're got an illegal declaration there: You can't specify `int` as a generic type. Try `Integer` – Bohemian Dec 05 '11 at 00:09
  • *"Also, how can I set the properties of SomeObject inline? Is it possible in java?"* - totally orthogonal to your real question. Ask it in a different Question ... and take the time to expand on what you are really asking ... – Stephen C Dec 05 '11 at 01:14

2 Answers2

3

Make it static, final, initialize it and use ConcurrentHashMap.

You can even have a singleton class for this, and make it lazy.

Something like this. But again, use ConcurrentHashmap. Dont use Hashtable.

import java.util.*;

class Foo { 
        private static final Foo foo = new Foo(); 
        public static final Hashtable table = new Hashtable(); 
        private Foo(){} 

        public static Foo Instance(){
            return foo;
        }

} 
DarthVader
  • 52,984
  • 76
  • 209
  • 300
0

I want to initialize a hashtable inside my class only once for my web application.

If you only want there to be one instance for your webapp then your choices are:

  • a bare static (nasty!!),

  • a static wrapped by a class with static methods (nasty, and awkward to use),

  • an static wrapped using the Singleton class pattern (not so nasty / awkward, but not without problems), or

  • a shared instance that is created and injected using a dependency injection (DI) framework.

The last is best from a number of perspectives, but it entails changing your webapp to use DI, which will be disruptive.

So I suggest that you read up on the Singleton class pattern and how to implement it in Java so that it is created and initialized in a thread-safe fashion:

Then there is the separate question/issue of how you use the hash table in a thread-safe fashion. And the answers/solutions depend on how your application is going to use the hash table once it has been initialized.

  • If the webapp only ever reads the hash table, then a plain HashMap is sufficient.

  • If the webapp needs to read and update the hash table, then the choice is between a ConcurrentHashMap or a synchronized wrapper for a regular HashMap.

    • If the table is likely to have lots of threads wanting to perform operations (i.e. lock contention is a likely issue), OR if you need to be able to iterate the table entries while updates might be happening, then use a ConcurrentHashMap.

    • Otherwise, a HashMap with a synchronization wrapper will be sufficient.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216