Greetings! Protecting shared, mutable data is difficult. Easiest way to guarantee thread safety is not to share. This technique is called thread confinement. One way to achieve this is using ThreadLocal class. This uses internal thread-local variable to store data by the executing thread and provides get() and set() methods to access it. It internally get the current thread (Thread.currentThread()) and a custom Map to assign data directly in Thread class. This behaves like Map<Thread, T> object (though this is not how it is done). ThreadLocal stores per-thread value and provides set(), get() methods to access it. public class TimeHolder { private static ThreadLocal<Long> threadLocal = ThreadLocal.withInitial(() -> System.currentTimeMillis()); public Long get() { return threadLocal.get(); } public void set() { threadLocal.set(System.currentTimeMillis()); } public void print() { System.out.println
May all beings be happy, be well, be peaceful, and be free