I have an anonymous class which takes the iterated value from a collection as shown below. With this code, is that immediate variable in
preserved within anonymous class? Or different threads can take same in
value?
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 20; i++)
list.add(i);
for (final Integer in : list) {
new Thread(new Runnable() {
@Override
public void run() {
Thread.sleep(1000);
System.out.println(value + "," + in);
}
}).start();
}