Thread safety issue
问题 I have a LinkedList with Objects, that I want to process. Objects get added to it from another thread, but only one Thread removes/reads from it. private LinkedList<MyObject> queue = new LinkedList<>(); new Thread() { @Override public void run() { while (!Thread.interrupted()) { if (!queue.isEmpty()) { MyObject first = queue.removeFirst(); // do sth.. } } } }.start(); In another Thread I add Objects to the queue queue.add(new MyObject()); Sometimes this code leads to an Exception though,