Pergunta de entrevista da empresa Yahoo

Design a singleton to return an object for each of the thread

Resposta da entrevista

Sigiloso

15 de dez. de 2009

Using ThreadLocal can achieve this very easily. Make the desired singleton class extend ThreadLocal and override the lazy initialization initialValue() method. If the class is not allowed, using a ConcurrentHashMap with the Thread as key and the singleton class instance as value. If not using Java, i think c++ has a built-in thread local keyword that can mark the class thread local.