Threadsafe vs Synchronized

前端 未结 7 1157
刺人心
刺人心 2021-02-02 11:01

I\'m new to java. I\'m little bit confused between Threadsafe and synchronized. Thread safe means that a method or class instance can be used by multiple threads at the same tim

7条回答
  •  离开以前
    2021-02-02 11:38

    Nayak, when we declare a method as synchronized, all other calls to it from other threads are locked and can wait indefinitely. Java also provides other means of locking with Lock objects now.

    You can also declare an object to be final or volatile to guarantee its availability to other concurrent threads.

    ref: http://www.javamex.com/tutorials/threads/thread_safety.shtml

提交回复
热议问题