高并发小设计思维
今天看sun的HttpMessages.java文件,虽然文件很小,但是对我的启发很大。
前面定义了大量的HTTP常量:
Java代码
public static String getMessage(int status) { // Return the status message for the most frequently used status // codes directly, without any lookup switch (status) { case 200: return STATUS_200; case 302: return STATUS_302; case 400: return STATUS_400; case 404: return STATUS_404; } return httpStatusCodeMappings.get("sc."+ status); } public static String getMessage(int status) { // Return the status message for the most frequently used status // codes directly, without any lookup switch (status) { case 200: return STATUS_200; case 302: return STATUS_302; case 400: return STATUS_400; case 404: return STATUS_404;} return httpStatusCodeMappings.get("sc."+ status); }
这段代码的用途很简单,就是要返回状态码对应的Message,而这些这些消息都已经存放在httpStatusCodeMappings变量里了,为了提高访问常用的几个返回码的Message,它直接用了一个静态块,而不去lookup那个map了,这就是高手!
sw1982 写道
...lookup 一下hashmap真的那么低效吗? 建议复习下数据结构哦,你这些总结是没错,可是很表面
典型的没有写过高并发程序的思维方式,明明可以节约的地方,仅仅几行代码就可以优化,偏偏不做。
hashmap再快,也比case 一个 整型满上1w倍。
性能,是一点一点挤牙膏挤出来的,哪能到处浪费啊。