线程工作不正常(同步)

Thread is not working properly (synchronization)

当我运行这段代码时,输​​出是"something is added"然后无限循环...

我的程序应该打印:

添加了一些东西

打印了一些东西

我不明白为什么程序不能在while循环中退出

import java.util.LinkedList;
public class Test {
static LinkedList<String> list = new LinkedList<String>();
public static void main(String[] args) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while(list.isEmpty()); // here is the loop
            System.out.println("something is printed"+list.get(0));
        }
    }).start(); 
    new Thread(new Runnable() {

        @Override
        public void run() {
            try{
                Thread.sleep(200);
                list.add("something");
                System.out.println("something is added");
            }catch (Exception e) {}
        }
    }).start(); 
  }
}

我正在寻找解决方案和解释

非常感谢

为了让线程安全地通信和共享数据,它们必须被适当地同步。在您的示例中,最简单的方法是将列表包装在同步包装器中:

static List<String> list = Collections.synchronizedList(new LinkedList<>());