Singleton « Development « Java Thread Q&A

Home
Java Thread Q&A
1.concurrency
2.Development
3.Exception
4.Notify
5.Operation
6.Socket
7.State
8.synchronize
9.Thread Safe
10.ThreadPool
Java Thread Q&A » Development » Singleton 

1. singletons and threads    stackoverflow.com

My question is about threads being queued. For my example I have one Spring context. I have a method named CalculateTax in a stateless class. A request comes in, a thread ...

2. Singleton class design - null object    stackoverflow.com

Me and a colleague are having a debate: We have a singleton class that is used at multiple places in our code base. Originally, the class was designed in such a way that ...

3. question about singleton classes and threads    stackoverflow.com

I'm trying to learn about singleton classes and how they can be used in an application to keep it thread safe. Let's suppose you have an singleton class called ...

4. java programing and the problem java singleton multithreading (singleton vs multithreading )    stackoverflow.com

I have a program which uses singleton pattern. I need to use threads with it taking in mind the output should be the same before and after using threads mechanize. ...

5. Singleton pattern using its methods in different threads    stackoverflow.com

When designing a singleton class which can be used by multiple threads I run into the following challenge: There exits a main thread and another thread called client. The main method first ...

6. In Java, it's possible Singleton per thread?    stackoverflow.com

Using threads, I have a principal class (SlaveCrawler) that instantiates three classes(Downloader, ContentAnalyzer, URLAnalyzer) that are dependent on each other. SlaveCrawler uses Downloader and URLAnalyzer Downloader uses ContentAnalyzer and URLAnalyzer ContentAnalyzer uses URLAnalyzer I want ...

7. Singleton in Multithreaded - Various Scenarios    stackoverflow.com

How below 2 codes are different in terms of multithreaded environment? Code 1:

public class Singleton {
private Singleton() {}

private static class SingletonHolder { 
    private static final Singleton INSTANCE = ...

8. implement a singleton design pattern as a template    stackoverflow.com

Here is the question: Implement a singleton design pattern as a template such that, for any given class Foo, you can call Singleton::instance() and get a pointer to an instance of a singleton of ...

9. Per thread singleton pattern    stackoverflow.com

In my work I stumbled upon such a design issue:

  • I need one instance of a Manager class per thread
  • These instances should be globally accessible, like in the singleton pattern via a ...

10. How to do eager initialization of singleton in a separate thread?    stackoverflow.com

I would like to do eager initialization of multiple singletons but the initialization can take on the order of minutes so I would like the initialization to happen on a separate ...

11. Are there any issues in multithreaded environment with Singleton pattern?    stackoverflow.com

I have implemented the Singleton class as below:

public class Singleton {

    private static  Singleton instance = null;


    private Singleton() { 
    ...

12. singleton with thread and multiple VM's    coderanch.com

We are using a singleton that starts a thread when the application initializes. The thread consumes records from a database table and calls a webservice for each record. What is the best way to migrate this scenario to multiple application servers and thus multiple virtual machines. The web service should be only called once for each record in the database. Best ...

13. Singletons and thread    coderanch.com

Hi I have a basic question regarding threads. I know about thread and the allied concepts. I want to clarify this to get to understand things better 1. I understand every object has its one data are on the heap memory so where is the confusion and need for synchronzing threads for not affecting different objects dataarea? 2. What is a ...

14. Can a Thread be a Singleton?    coderanch.com

Hi all, Please advise if there is a problem in creating a Singleton Thread. The only doubt I have in mind is what will happen if the instance of the Singleton Thread is in its run method and another Object gets access to the thread by singletonThread = SingletonThread.getInstance() call and then invokes its run method? Please advise. Regards, Murad.

15. Singleton & multithread env.    coderanch.com

Hallo, I have a singleton used in multithread environment see the code : public class Registry { /** * The Registry is a singleton and here * it holds its unique instance. */ private static Registry registry; /** * Here are the registerd values stored. */ private final WeakHashMap values; /** * Gets a Registry instance. * * @return Registry */ ...

16. Singleton Design Pattern    coderanch.com

A fellow developer in our group asked me an interesting question about the Singleton Design Pattern. I have a Singleton class and I am posting the pertinent code below: class DataSchema { /** * The only instance created, accessed using the static getInstance method */ private static DataSchema theInstance; /** * classLock object stores the class type of the DataSchema class ...

17. Question regarding Singleton pattern    coderanch.com

In the double-checked locking implementation mentioned in Head First Design Patterns, I don't understand why the static variable holding the Singleton instance needs to be volatile. I think it would work fine, even if it is not volatile. Can some one explain why this code needs the variable to be volatile? Also, I am not able to clearly understand why this ...

18. The method in a singleton class    coderanch.com

Hi Folks, There is a public calculate() method a singleton class. If there are 2 threads to call the calculate() at the same time. I wonder if one thread must wait until another thread executing calculate() is finished? If it is not a singleton class. I changed the mthod to static public calculate(). I still wonder if one thread must wait ...

19. Multi-threads access singleton    coderanch.com

Dear all, I have a problem. If a singleton contains a Hashmap variable and it is accessed by different threads. Different threads put or get value to/from the Hashmap through the singleton reference. At some time, a thread cannot get a value from the Hashmap although this value is stored to Hashmap with the key before. Below are the sources : ...

20. Singleton and Multiple threads    coderanch.com

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 ...

21. Multi-Threaded program using a singleton object    coderanch.com

Hi, This is a question about a multi-threaded program. Imagine we have a singleton class and we call an instance of it from 2 different threads. Both threads will now have a reference to the same object in memory. If the singleton class has a mehod which isn't synchronized and it accepts 2 numbers and just adds them. If we call ...

22. Singleton in multithreads    coderanch.com

public class Singleton { private static Singleton instance = null; [B]private static XYZXlass xyz = null;[/B] private Singleton() { } public static Singleton getInstance() throws Exception { synchronized(Singleton.class) { if(instance == null) instance = new Singleton(); [B]if(xyz == null) xyz = new XYZXlass("abc","efg");[/B] } return instance; } [B] public XYZXlass getXyz() { return xyz; } [/B] }

23. singleton class can be mutithreaded?    coderanch.com

Hello Jacob, even a singleton object may be accessed simultaneously by multiple threads. You still have to take care of correct synchronization if you want to make access to it thread-safe. And there are subtle difficulties to create a real singleton in a multi-threaded application. You can't just use the typical singleton pattern if you want to guarantee that there will ...

24. Invoking Singleton Method In Thread    coderanch.com

Hi all, I have a singleton object with a public non-static method, let's say getX(). If I would to invoke the getX() method with 10 concurrent threads, would there actually be 1 instance of getX() method or 10 instances ? I did some simple experiment by putting some states in the getX() method and it does seemed that there are 10 ...

25. 'singleton objects' to coordinate read/write    coderanch.com

I am developing a little web application where each session will have access to a large number of documents shared in the app. The documents are immutable and their object representations are immutable. To minimize and control the number of documents in memory, I have created a singleton DocBank that has a map of all documents in memory. To synchronize reading ...

26. Can a class be singleton after implements Runnable?    coderanch.com

In test1, there is only one instance of MyClass to create new threads but in test2 method, a new instance of MyClass is created to create new threads. Hold on..You are mixing up concepts here - singletons with runnable objects The idea behind creating a thread object with a runnable object passed to its class constructor (the pattern you follow in ...

27. Singleton pattern?    coderanch.com

Hi, My first post here - sorry if its a little complicated. I'm designing an application which I want to store a chunk of XML in memory. All threads (servlets) can access this and it only changes infrequently. I thought about using a singleton pattern for this (static stuff) but I've read that there can be problems with that when multiple ...

29. Singleton with a single worker thread    coderanch.com

Hello, In a Servlet environment I try to implement a Messenger class for the jobs of taking messages to its queue and sending them to online users. Messenger is a Singleton implementing Runnable. I can't find a proper way to .start() the inner thread for checking the queue and sending the message (in an another thread). Can you please comment on ...

30. Singleton Class with Threads    coderanch.com

If a singleton is not thread safe, and two threads enter getInstance() at the same time, there are chances for getting two different objects, provided getInstance() is not synchronized. Is this true first of all. If so, how to recreate it? I did try with the below program, package test.threads; class SingleTonThreads { private SingleTonThreads(){ } static SingleTonThreads tstt; public static ...

31. threadsafety of a singleton    coderanch.com

The "final" attribute has nothing to do with synchronization; it simply tells the compiler that no other code is allowed to assign a new value to that variable. Clearly that's an important design consideration for a singleton object. But since the variable is private, only code in the Singleton class could do that, and you can inspect the code to see ...

32. I got a problem: Singleton and Thread...    forums.oracle.com

33. Does a singleton thread make sense?    forums.oracle.com

I'm new to developing with threads and the project I'm on has some Thread classes that are implemented as singletons. Does this make sense to do? It seems like from what I'm seeing (since thread.stop() is deprecated) that we should start a thread going and just let it finish. When I tried working with this thread, it would never finish since ...

34. basic query about Singleton class and threads    forums.oracle.com

if i have a singleton class, and multiple threads can have access to my class, will the single instance be shared among the threads? Does that mean that the value of the data members of the class are one and the same, for the threads? The context for me is like this. Client -----> library -----> server <----- <------ My library ...

35. Problem Multithreading - Singleton    forums.oracle.com

Thanks all for spending your time helping me. First: when one thread ask for work and it's null then it will sleep(), it will not exit (like dwg suggest). The thread ends when all the program shutdowns (a clean shutdown). Second: I try to sync the getWork() function in the singleton to test the behavior. I'm using the singleton thread as ...

36. Singleton class using threads    forums.oracle.com

Hi all, I have one singleton class like Connection ( this has the databse connection). if everytime I need a connection I called the Connection.getConnection() method. But the problem is , in My applicaiton I am using the Threading. If i use the threading then its getting the null pointer exception in the Connection class. I think the Singleton class is ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.