Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, it's possible Singleton per thread?

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

SlaveCrawler uses Downloader and URLAnalyzer

Downloader uses ContentAnalyzer and URLAnalyzer

ContentAnalyzer uses URLAnalyzer

I want only one instance of each class. If I use Singleton, I can get this, but working with threads, I will have 20 SlaveCrawlers(example), so I want 20 URLAnalyzer.

It's possible make this using Singleton or I need other way?

like image 307
Renato Dinhani Avatar asked May 20 '11 00:05

Renato Dinhani


People also ask

Can multiple threads access a singleton?

It can be used in a single threaded environment because multiple threads can break singleton property as they can access get instance method simultaneously and create multiple objects.

Are singleton objects shared across threads?

In a Singleton Object you have: Fields: They are stored in memory. They can be shared amongst multiple threads and you have no guarantee they will keep consistent (unless you make them synchronized).

Is singleton is thread safe in Java?

Is singleton thread safe? A singleton class itself is not thread safe. Multiple threads can access the singleton same time and create multiple objects, violating the singleton concept. The singleton may also return a reference to a partially initialized object.

How do you handle singleton in multithreading?

Thread Safe Singleton in JavaCreate the private constructor to avoid any new object creation with new operator. Declare a private static instance of the same class. Provide a public static method that will return the singleton class instance variable.


2 Answers

Take a look at ThreadLocal. Each thread will have its own local copy of each object.

ThreadLocal<YourObject> threadLocalYourObject = new ThreadLocal<YourObject>() {
  @Override
  protected YourObject initialValue() {
    //initialize YourObject
  }
}

Or in 1.8 we can use:

ThreadLocal<YourObject> threadLocalYourObject = ThreadLocal.withInitial( () -> new YourObject() )

To get access to your ThreadLocal object, use the get() method.

YourObject yourObject = threadLocalYourObject.get();
like image 141
nicholas.hauschild Avatar answered Oct 18 '22 00:10

nicholas.hauschild


You can implement it with ThreadLocal. Here is the pseudo code:

public class ThreadLocalTest {

  public static void main(String[] args){
    MyTLSingleTon obj = MyTLSingleTon.getInstance();
  }

}

class MyTLSingleTon{

  private MyTLSingleTon(){  
  }

  private static final ThreadLocal<MyTLSingleTon> _localStorage = new ThreadLocal<MyTLSingleTon>(){
    protected MyTLSingleTon initialValue() {
      return new MyTLSingleTon();
   }
  };

  public static MyTLSingleTon getInstance(){
    return _localStorage.get();
  }
}

MyTLSingleTon.getInstance(); method will return an object assosiated with the current thread. and if no object is assosiated than protected MyTLSingleTon initialValue() method will be called and a new instance will be set.

like image 5
Nirmit Shah Avatar answered Oct 18 '22 01:10

Nirmit Shah