threads and singleton objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: threads and singleton objects

  1. #1
    Join Date
    Feb 2004
    Posts
    111

    threads and singleton objects

    If I have a singleton object that is created when a .NET web application is started and then I spawn a new thread from that application, can the child thread see and use the singleton object?

    Laurence -

  2. #2
    Join Date
    Jan 2007
    Posts
    112
    I believe so. Basically you want to know if you create an object on one thread can another thread use it. I've done this without a problem. The only thing to consider is possible syncronization issues. If any possible issues exist they'll need to be taken care of some how.

  3. #3
    Join Date
    Nov 2004
    Location
    Oslo
    Posts
    73
    Also remember that if the singleton is a form, then you need to use Invoke / BeginInvoke if you are gonna do any GUI-work from the other thread.
    oes

  4. #4
    Join Date
    Feb 2004
    Posts
    111
    What I'm going to trying to do is store used command objects within a collection contained within the singleton object. The thread is there to monitor the singleton object and remove command objects that aren't used within a given amount of time (to prevent what could be a massive memory leak).

    I figure that way, I'll save the time required to constantly recreate and configure command objects for those stored procedures that are most commonly used by the application as it runs.

    Laurence -

  5. #5
    Join Date
    Jun 2004
    Posts
    93
    Threading does not affect scope, so as long as the singleton object can be seen by the thread routine, you are OK.
    If you will have multiple threads accessing the object, make sure you read up on syncronization and possible issues.

Similar Threads

  1. Java Singleton Pattern Tutorial
    By hemanthjava in forum Java
    Replies: 2
    Last Post: 11-14-2006, 10:57 PM
  2. Replies: 0
    Last Post: 04-29-2002, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center