在后端服务中,如果使用static做flag控制或做数据共享时,在高并发中有可能会遇到在A线程已经变更但在B线程中读到的还是脏数据,这是因为JAVA中每个线程都有一个私有线程,会复制主内存中变量做副本到私有线程中,当A线程更新静态变量时,先更新的是本地私有内存中的副本变量,然后再同步到主内存中,如:
private static boolean stopRequested; public void testStatic() { try { Thread currThread = new Thread(new Runnable(){ public void run(){ System.out.println("开始:" + System.currentTimeMillis()); int i = 0; while (!stopRequested){ i++; // try { // Thread.sleep(1); // } catch (InterruptedException e) { // e.printStackTrace(); // } } System.out.println(System.currentTimeMillis()); } }); currThread.start(); TimeUnit.SECONDS.sleep(1); stopRequested = true; System.out.println(System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } }
这种情况下,主线程静态变量stopRequested变更后,currThread线程是没有监听到,程序变成了死循环。放开注释后,会在stopRequested值变更后,获取到主内存的值并结束程序。至于为什么放注释后,猜测是在持续不间断读取变量时,应该是先读取本地副本,在有等待时间时,会更新静态变量的主内存数据到线程副本中.
volatile变量会将副本中更新到主内存中,而每次读取都是主内存中的数据,这样就保证了数据的更新会使用其它线程可见。同样上面的代码,在使用volatile不放开注释就不会有问题。