Created
December 3, 2022 08:23
-
-
Save yu1745/5d8c7a61e2fb4c46e499a10676d2ead9 to your computer and use it in GitHub Desktop.
同步方法会引起缓存失效
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//现在的代码,一秒后停止运行,因为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