Register now or log in to join your professional community.
Is is possible using transient
Yes, Using transient keyward
If we are working servlet then we can can extend with SingleThreadClass. Or for genereal core application don't create any class level variable.
ReentrantLock or build you own locking mechanism
Well there are many ways you can achieve this, but each contains many flavors. Java 8 also ships with new concurrency features. Some ways you could make sure thread safety are: Semaphores Locks-Reentrantlock,ReadWriteLock,StampedLock(Java 8)
by using local variables or java.lang.ThreadLocal
Some other useful facts about thread-safe
Local variables are thread safe in Java.
Each thread has its own stack. Two different threads never shares the same stack. All local variables defined in a method will be allocated memory in stack. As soon as method execution is completed by the current thread, stack frame will be removed.