JDK1.8 有专门的注解 @Contended 来避免伪共享,为了更加直观,我使用了对象填充的方法,其中 protected long p1, p2, p3, p4, p5, p6, p7 作为前置填充对象,protected long p9, p10, p11, p12, p13, p14, p15作为后置填充对象,这样任意线程访问 ValuePadding 时,value 都处于不同的 Cache line 中,不会产生伪共享问题。
下面的例子用来演示伪共享与解决伪共享后的性能差异:
publicclassMyFalseSharing{
publicstaticvoidmain(String[] args)throws InterruptedException { for (int i = 1; i < 10; i++) { System.gc(); finallong start = System.currentTimeMillis(); runTest(Type.PADDING, i); System.out.println("[PADDING]Thread num " + i + " duration = " + (System.currentTimeMillis() - start)); } for (int i = 1; i < 10; i++) { System.gc(); finallong start = System.currentTimeMillis(); runTest(Type.NO_PADDING, i); System.out.println("[NO_PADDING] Thread num " + i + " duration = " + (System.currentTimeMillis() - start)); } }
privatestaticvoidrunTest(Type type, int NUM_THREADS)throws InterruptedException { Thread[] threads = new Thread[NUM_THREADS];
switch (type) { case PADDING: DataPadding.longs = new ValuePadding[NUM_THREADS]; for (int i = 0; i < DataPadding.longs.length; i++) { DataPadding.longs[i] = new ValuePadding(); } break; case NO_PADDING: Data.longs = new ValueNoPadding[NUM_THREADS]; for (int i = 0; i < Data.longs.length; i++) { Data.longs[i] = new ValueNoPadding(); } break; }
for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(new FalseSharing(type, i)); } for (Thread t : threads) { t.start(); } for (Thread t : threads) { t.join(); } }
Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。