Look at the code below:
public class Main {
private static final ThreadLocal<Integer> threadLocalCounter = new ThreadLocal<>();
public static void main(String[] args) throws InterruptedException {
class MyRunnable implements Runnable {
@Override
public void run() {
if (threadLocalCounter.get() != null) {
threadLocalCounter.set(threadLocalCounter.get() + 1);
} else {
threadLocalCounter.set(0);
}
}
};
Thread thread1 = new Thread(new MyRunnable(), "first_thread");
Thread thread2 = new Thread(new MyRunnable(), "second_thread");
Thread thread3 = new Thread(new MyRunnable(), "third_thread");
// first
thread1.start();
thread1.join();
// second
thread2.start();
thread2.join();
// third
thread3.start();
thread3.join();
}
}
What value of threadLocalCounter will each thread have?
Example of answer:
3 4 5