search

Home  >  Q&A  >  body text

java - spring中bean为单例为什么有助于线程无关性

怎么理解这句话:spring的bean都是单实例(singleton)的,其最大好处就是线程无关性? 我觉得如果是单实例的,那么对于某个bean,在spring容器中应该全局就一个bean实例。如果只有一个bean实例,多个线程都用它,才更会产生线程的冲突啊!

高洛峰高洛峰2767 days ago415

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:45:46

    Thread-independent means that each thread will not be allocated an instance, or the generation of instances has nothing to do with threads. But thread independence does not mean thread safety. Thread safety needs to be done by yourself

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:45:46

    Single instance beans are all stateless, so why do thread conflicts arise? If you use a stateful bean as a single instance, there is something wrong with your own logic

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:45:46

    This sentence is incomplete, singleton + stateless can be thread safe

    reply
    0
  • Cancelreply