Why, despite the synchronization, one time - First Name drops out earlier, and another time - null. In one of the outcomes, null is first displayed, and then First Name. How, after deleting the name, can we take and display First Name? Explain to me how to read this code correctly? For example, how does "return remove (0)" work? Thank.
package synchronized_collections; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main extends Thread { public static void main(String[] args) throws InterruptedException { NameList nameList = new NameList(); nameList.addName("First Name"); class MyThread extends Thread { @Override public void run() { System.out.println(nameList.removeFirstName() + "\nРазмер после удаления - " + nameList.size()); } } new MyThread().start(); new MyThread().start(); } static class NameList extends Thread { private List<String> list = Collections.synchronizedList(new ArrayList<>()); public synchronized void addName(String name) { list.add(name); } public synchronized String removeFirstName() { if(list.size() > 0) { return list.remove(0); } System.out.println("----------------------------"); return null; } public synchronized int size() { return list.size(); } } }