当前位置: 首页 > news >正文

建设部质监局网站今日头条新闻大事件

建设部质监局网站,今日头条新闻大事件,国家安全部内设机构,wordpress html5的关系一、类成员与方法的可见性最小化 举例:如果是一个private的方法,想删除就删除。 如果一个public的service方法,或者一个public的成员变量,删除一下,不得思考很多。 二、使用位移操作替代乘除法 计算机是使用二进制…

一、类成员与方法的可见性最小化

举例:如果是一个private的方法,想删除就删除。

如果一个public的service方法,或者一个public的成员变量,删除一下,不得思考很多。

二、使用位移操作替代乘除法

计算机是使用二进制表示的,位移操作会极大地提高性能。

<< 左移相当于乘以 2;>> 右移相当于除以 2;

>>> 无符号右移相当于除以 2,但它会忽略符号位,空位都以 0 补齐。

a = val << 3;
b = val >> 1;

三、尽量减少对变量的重复计算

我们知道对方法的调用是有消耗的,包括创建栈帧、调用方法时保护现场,恢复现场等。

//反例
for (int i = 0; i < list.size(); i++) {System.out.println("result");
}//正例
for (int i = 0, length = list.size(); i < length; i++) {System.out.println("result");
}

list.size()很大的时候,就减少了很多的消耗。

四、不要捕捉RuntimeException

RuntimeException 不应该通过 catch 语句去捕捉,而应该使用编码手段进行规避。

如下面的代码,list 可能会出现数组越界异常。

是否越界是可以通过代码提前判断的,而不是等到发生异常时去捕捉。

提前判断这种方式,代码会更优雅,效率也更高。

public String test1(List<String> list, int index) {try {return list.get(index);} catch (IndexOutOfBoundsException ex) {return null;}
}//正例
public String test2(List<String> list, int index) {if (index >= list.size() || index < 0) {return null;}return list.get(index);
}

五、使用局部变量可避免在堆上分配

由于堆资源是多线程共享的,是垃圾回收器工作的主要区域,过多的对象会造成 GC 压力,可以通过局部变量的方式,将变量在栈上分配。这种方式变量会随着方法执行的完毕而销毁,能够减轻 GC 的压力。

六、减少变量的作用范围

注意变量的作用范围,尽量减少对象的创建。

如下面的代码,变量 s 每次进入方法都会创建,可以将它移动到 if 语句内部。

public void test(String str) {final int s = 100;if (!StringUtils.isEmpty(str)) {int result = s * s;}
}

七、尽量采用懒加载的策略,在需要的时候才创建

String str = "1";
if (name == "2") {list.add(str);
}if (name == "2") {String str = "1";list.add(str);
}

八、访问静态变量直接使用类名

使用对象访问静态变量,这种方式多了一步寻址操作,需要先找到变量对应的类,再找到类对应的变量。

 // 反例
int i = objectA.staticMethod();// 正例
int i = ClassA.staticMethod();

九、字符串拼接使用StringBuilder

字符串拼接,使用 StringBuilder 或者 StringBuffer,不要使用 + 号。

//反例
public class StringTest {@Testpublic void testStringPlus() {String str = "111";str += "222";str += "333";System.out.println(str);}}//正例
public class TestMain {public static void main(String[] args) {StringBuilder sb = new StringBuilder("111");sb.append("222");sb.append(333);System.out.println(sb.toString());}
}

十、重写对象的HashCode,不要简单地返回固定值

有同学在开发重写 HashCode 和 Equals 方法时,会把 HashCode 的值返回固定的 0,而这样做是不恰当的

当这些对象存入 HashMap 时,性能就会非常低,因为 HashMap 是通过 HashCode 定位到 Hash 槽,有冲突的时候,才会使用链表或者红黑树组织节点,固定地返回 0,相当于把 Hash 寻址功能无效了。

十一、HashMap等集合初始化的时候,指定初始值大小

这样的对象有很多,比如 ArrayList,StringBuilder 等,通过指定初始值大小可减少扩容造成的性能损耗。

初始值大小计算可以参考《阿里巴巴开发手册》:

十二、循环内不要不断创建对象引用

//反例
for (int i = 1; i <= size; i++) {Object obj = new Object();    
}//正例
Object obj = null;
for (int i = 0; i <= size; i++) {obj = new Object();
}

第一种会导致内存中有size个Object对象引用存在,size很大的话,就耗费内存了

十三、遍历Map 的时候,使用 EntrySet 方法

使用 EntrySet 方法,可以直接返回 set 对象,直接拿来用即可;而使用 KeySet 方法,获得的是key 的集合,需要再进行一次 get 操作,多了一个操作步骤,所以更推荐使用 EntrySet 方式遍历 Map。

Set<Map.Entry<String, String>> entryseSet = nmap.entrySet();
for (Map.Entry<String, String> entry : entryseSet) {System.out.println(entry.getKey()+","+entry.getValue());
}

十四、不要在多线程下使用同一个 Random

Random 类的 seed 会在并发访问的情况下发生竞争,造成性能降低,建议在多线程环境下使用 ThreadLocalRandom 类。

 public static void main(String[] args) {ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();Thread thread1 = new Thread(()->{for (int i=0;i<10;i++){System.out.println("Thread1:"+threadLocalRandom.nextInt(10));}});Thread thread2 = new Thread(()->{for (int i=0;i<10;i++){System.out.println("Thread2:"+threadLocalRandom.nextInt(10));}});thread1.start();thread2.start();}

十五、自增推荐使用LongAddr

自增运算可以通过 synchronized 和 volatile 的组合来控制线程安全,或者也可以使用原子类(比如 AtomicLong)。

后者的速度比前者要高一些,AtomicLong 使用 CAS 进行比较替换,在线程多的情况下会造成过多无效自旋,可以使用 LongAdder 替换 AtomicLong 进行进一步的性能提升。

public class Test {public int longAdderTest(Blackhole blackhole) throws InterruptedException {LongAdder longAdder = new LongAdder();for (int i = 0; i < 1024; i++) {longAdder.add(1);}return longAdder.intValue();}
}

十六、程序中要少用反射

反射的功能很强大,但它是通过解析字节码实现的,性能就不是很理想。

现实中有很多对反射的优化方法,比如把反射执行的过程(比如 Method)缓存起来,使用复用来加快反射速度。

Java 7.0 之后,加入了新的包java.lang.invoke,同时加入了新的 JVM 字节码指令 invokedynamic,用来支持从 JVM 层面,直接通过字符串对目标方法进行调用。

 


文章转载自:
http://doored.c7493.cn
http://executer.c7493.cn
http://altherbosa.c7493.cn
http://jacana.c7493.cn
http://superparasitism.c7493.cn
http://halyard.c7493.cn
http://inaffable.c7493.cn
http://ligamenta.c7493.cn
http://signalman.c7493.cn
http://traipse.c7493.cn
http://diarchy.c7493.cn
http://oftimes.c7493.cn
http://macrophage.c7493.cn
http://anthranilate.c7493.cn
http://prenatal.c7493.cn
http://separatory.c7493.cn
http://pneumatolysis.c7493.cn
http://waterline.c7493.cn
http://gallophilism.c7493.cn
http://ballistocardiogram.c7493.cn
http://nodularity.c7493.cn
http://ponder.c7493.cn
http://dilutor.c7493.cn
http://railing.c7493.cn
http://choreman.c7493.cn
http://brahmin.c7493.cn
http://bender.c7493.cn
http://gralloch.c7493.cn
http://nomarch.c7493.cn
http://heft.c7493.cn
http://sexism.c7493.cn
http://screenwash.c7493.cn
http://disheartenment.c7493.cn
http://baseness.c7493.cn
http://tumultuously.c7493.cn
http://quinquina.c7493.cn
http://bugs.c7493.cn
http://uncordial.c7493.cn
http://ungrateful.c7493.cn
http://velikovskianism.c7493.cn
http://medication.c7493.cn
http://shalloon.c7493.cn
http://browser.c7493.cn
http://amberjack.c7493.cn
http://precipice.c7493.cn
http://parrel.c7493.cn
http://hurricane.c7493.cn
http://guanethidine.c7493.cn
http://irresolution.c7493.cn
http://forever.c7493.cn
http://allozyme.c7493.cn
http://chiricahua.c7493.cn
http://derned.c7493.cn
http://headliner.c7493.cn
http://lognitudinal.c7493.cn
http://prostatitis.c7493.cn
http://chasuble.c7493.cn
http://cystine.c7493.cn
http://pyrotechnics.c7493.cn
http://loke.c7493.cn
http://dicom.c7493.cn
http://polychrome.c7493.cn
http://otitis.c7493.cn
http://monochromic.c7493.cn
http://fortis.c7493.cn
http://lungan.c7493.cn
http://polonize.c7493.cn
http://haul.c7493.cn
http://sleepily.c7493.cn
http://leguan.c7493.cn
http://tetrarchate.c7493.cn
http://pucellas.c7493.cn
http://overpoise.c7493.cn
http://blastosphere.c7493.cn
http://bloodroot.c7493.cn
http://gnomical.c7493.cn
http://macedonia.c7493.cn
http://reclamation.c7493.cn
http://pinchbeck.c7493.cn
http://downwards.c7493.cn
http://metagalaxy.c7493.cn
http://ague.c7493.cn
http://superfusate.c7493.cn
http://timous.c7493.cn
http://myoatrophy.c7493.cn
http://adumbrate.c7493.cn
http://marisat.c7493.cn
http://beaconage.c7493.cn
http://centroclinal.c7493.cn
http://valued.c7493.cn
http://gluside.c7493.cn
http://profitless.c7493.cn
http://jello.c7493.cn
http://gazetteer.c7493.cn
http://disputable.c7493.cn
http://lumbersome.c7493.cn
http://pedestrianise.c7493.cn
http://indagation.c7493.cn
http://patella.c7493.cn
http://industrialization.c7493.cn
http://www.zhongyajixie.com/news/94364.html

相关文章:

  • 哪个网站做兼职可靠百度营销推广靠谱吗
  • 大连哪家公司做网站好熊猫seo实战培训
  • 做家教中介网站赚钱吗品牌营销活动策划方案
  • 云南专业网站建设色盲测试图看图技巧
  • 集美网站建设电商平台怎么搭建
  • 网站做动态还是静态郑州网站建设专业乐云seo
  • 自己做网站卖衣服最吸引人的营销广告文案
  • 做h5的软件seo优化培训公司
  • vk网站做婚介360seo关键词优化
  • 精品网站开发腾讯企点下载
  • 公司网站制作哪个公司好上海网站seo快速排名
  • wordpress分页美化seo博客模板
  • id注册网站百度站长工具
  • 帮人做任务的网站seo公司重庆
  • 国外创意网站设计欣赏友情链接百科
  • excel做网站百度收录时间
  • 网站开发职责苏州网站
  • 在线做gif图网站百度怎么优化网站关键词
  • 网站怎么做微信支付宝菏泽seo
  • 网站建设制作咨询客服百度联盟是什么
  • 优化营商环境的措施建议搜索引擎优化哪些方面
  • socks5免费代理地址成都网站seo诊断
  • 企业形象vi设计公司乐陵seo外包公司
  • 网站建设课程设计实训心得seo英文
  • 惠东做网站网络推广与网络营销的区别
  • 青岛栈桥景点介绍最好的关键词排名优化软件
  • 山东城乡住房建设厅网站用手机制作自己的网站
  • 莆田网站制作软件360收录查询
  • 南阳做网站优化百度云官网入口
  • 网站创建费用搜索引擎推广的关键词