Posts

Showing posts with the label java

Thread local in java

Thread Local The ThreadLocal class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to aThreadLocal variable, then the two threads cannot see each other's ThreadLocal variables. public class Threadlocal {     public static class MyRunnable implements Runnable {         private int id;         public MyRunnable(int id) {             // TODO Auto-generated constructor stub             this.id = id;         }         private ThreadLocal threadLocal = new ThreadLocal ();         @Override         public void run() {             threadLocal.set(id);             try { ...