tags:

views:

39

answers:

2

I have been developing a project and in this project i have designed my code to do the same job after a specified time interval continuously. The job that wanted to be done has a lot of distinct cycles. The interval is small to execute them normally thus i used threads. Until that point everything is clear for me.

To decrease the process and information transaction i wanted to put an session like object that holds the given data and provide it to any thread at anytime. With this object i plan to not query the same configuration information from database at everytime but if it exists on the session take it else query and store on session.

I'm not sure how to implement this structure.

Regards,

+1  A: 

Have you looked at ThreadLocal?

matt b
nope, cheking out.
scriptmonster
A: 

That depends. There are several ways to keep and pass information in Java.

  1. Applicationwide: declare it static and/or load it in a static {}.
  2. Threadlocal: make use of ThreadLocal<T>.
  3. Objects: put data in wrapper objects (javabeans?) which you just create once and pass around as c'tor/method arguments.

In your case I think either 1 or 3 is applicable. A real "session" is usually threadlocal, but your functional requirement ("provide to any thread at anytime", "configuration information") makes me think you're rather looking for an applicationwide constant.

BalusC
1. I don't want to use directly shared memory.2. 3. I'm checking.
scriptmonster
Well, then go for 3. Have a "mother" thread or object wherein you store this information and pass through to another threads as c'tor/method argument.
BalusC
As you mentioned i need an application wide object that could be accessible from any object in application. i have investigated all ways that you mentioned. At first i asked this question to see other ways of doing this with an object that can be used from all objects. But 3rd way seems to be the best for my case.
scriptmonster