Skip to content

Instantly share code, notes, and snippets.

@yu1745
Created December 3, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yu1745/5d8c7a61e2fb4c46e499a10676d2ead9 to your computer and use it in GitHub Desktop.
Save yu1745/5d8c7a61e2fb4c46e499a10676d2ead9 to your computer and use it in GitHub Desktop.
同步方法会引起缓存失效
//现在的代码,一秒后停止运行,因为println是同步方法引起本地缓存失效
//把第一条println注释掉,第二条println解除注释,就会一直运行
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class Test {
static boolean b = true;
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
while (b) {
System.out.println("running");
/*try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}*/
}
// System.out.println("done");
}).start();
TimeUnit.SECONDS.sleep(1);
b = false;
//防止jvm退出
new CountDownLatch(1).await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment