aspose file tools*
The moose likes Threads and Synchronization and the fly likes Singleton and Multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Singleton and Multiple threads" Watch "Singleton and Multiple threads" New topic
Author

Singleton and Multiple threads

M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Hi,

I understand that a Singleton can only have one instance at any given time.

My question is can multiple threads use the same instance? If so what are the thread-safety issues with this? specifically:

1- Would class static variables be shared amongst the threads?
2- Would local variables to non staic methods be shared amongst the threads?



The reason I ask this is because I have designed a Data Access Facade Layer as a singleton. During instantiation it would initialize the data source, as I don't want it to be initialized more than once. Then I have member methods (instance methods) which perform insert, select, update ...etc to the database. Each of those methods gets its own connection, statement, resultset ...etc

Was just wondering if the above has any thread safety issues as it will be used by multiple threads.


Thanks for replying in advance

P.S. The above class will be running in Tomcat and called from a servlet, not sure if this is relevent.


SCJP J2SE 1.4<br />SCBCD J2EE 1.3
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Here's the thread tutorial that will probably answer many of your questions:

http://java.sun.com/docs/books/tutorial/essential/threads/

Yes, class variables can be accessed by all threads. Likewise instance variables. Local variables are not shared among all threads (but it's possible for them to reference objects that are being accessed by several threads, so you still have to take care.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton and Multiple threads