今天看sun的HttpMessages.java文件,虽然文件很小,但是对我的启发很大。
前面定义了大量的HTTP常量:
Java代码
...
...
private static final String STATUS_305 = "Use Proxy";
private static final String STATUS_307 = "Temporary Redirect";
private static final String STATUS_400 = "Bad Request";
private static final String STATUS_401 = "Unauthorized";
private static final String STATUS_402 = "Payment Required";
private static final String STATUS_403 = "Forbidden";
private static final String STATUS_404 = "Not Found";
private static final String STATUS_405 = "Method Not Allowed";
private static final String STATUS_406 = "Not Acceptable";
private static final String STATUS_407 = "Proxy Authentication Required";
private static final String STATUS_408 = "Request Timeout";
...
...
...
...
private static final String STATUS_305 = "Use Proxy";
private static final String STATUS_307 = "Temporary Redirect";
private static final String STATUS_400 = "Bad Request";
private static final String STATUS_401 = "Unauthorized";
private static final String STATUS_402 = "Payment Required";
private static final String STATUS_403 = "Forbidden";
private static final String STATUS_404 = "Not Found";
private static final String STATUS_405 = "Method Not Allowed";
private static final String STATUS_406 = "Not Acceptable";
private static final String STATUS_407 = "Proxy Authentication Required";
private static final String STATUS_408 = "Request Timeout";
...
...
也可以趁机从这里查看HTTP返回值的意义^_^。然后定义了一个java.util.concurrent.ConcurrentHashMap的变量httpStatusCodeMappings,再往里面添加刚才定义的常量(这个操作放在了一个静态块里),如:
Java代码
static {
httpStatusCodeMappings.put("sc.100", STATUS_100);
httpStatusCodeMappings.put("sc.101", STATUS_101);
httpStatusCodeMappings.put("sc.200", STATUS_200);
httpStatusCodeMappings.put("sc.201", STATUS_201);
httpStatusCodeMappings.put("sc.202", STATUS_202);
httpStatusCodeMappings.put("sc.203", STATUS_203);
...
...
static {
httpStatusCodeMappings.put("sc.100", STATUS_100);
httpStatusCodeMappings.put("sc.101", STATUS_101);
httpStatusCodeMappings.put("sc.200", STATUS_200);
httpStatusCodeMappings.put("sc.201", STATUS_201);
httpStatusCodeMappings.put("sc.202", STATUS_202);
httpStatusCodeMappings.put("sc.203", STATUS_203);
...
...
最重要的就是这个方法了,先看代码:
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倍。
性能,是一点一点挤牙膏挤出来的,哪能到处浪费啊。