Home  >  Q&A  >  body text

java - 单例模式中生成的对象是否都要是无状态的?

假设我2个线程同时对某个单例模式生成的对象进行操作,也就是操作的是同一个对象,那岂不是会造成此对象内部状态的不一致?

ringa_leeringa_lee2742 days ago504

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-18 10:57:45

    The singleton mode is inevitably accompanied by multi-threading problems. You can use appropriate locking mechanisms to control it, or ThreadLocal is also a solution. Specific problems should be analyzed in detail. Of course, if it is a stateless class, you don’t need to think so much.

    reply
    0
  • Cancelreply