小编Max*_*ler的帖子

为什么将类定义为final会改善JVM性能?

引自http://sites.google.com/site/gson/gson-design-document:

为什么Gson中的大多数课程都被标记为最终?

虽然Gson通过提供可插拔序列化器和反序列化器提供了相当可扩展的架构,但Gson类并未专门设计为可扩展.提供非最终类将允许用户合法地扩展Gson类,然后期望该行为在所有后续修订中工作.我们选择通过将类标记为final来限制这样的用例,并等到出现良好的用例以允许扩展性.标记类final也有一个很小的好处,即为Java编译器和虚拟机提供额外的优化机会.

为什么会这样?[如果我猜测:JVM知道类是最终的,它不维护方法覆盖表?还有其他原因吗?]

性能有什么好处?

这是适用于频率实例化的类(POJO?)还是适用于持有静态方法(实用类)的类?

定义为final的方法在理论上也可以提高性能吗?

有什么影响吗?

谢谢你,马克西姆.

java optimization performance jvm final

42
推荐指数
2
解决办法
1万
查看次数

如何使用apache http客户端库4.1设置自定义User-Agent?

如何使HTTPClient使用自定义User-Agent标头?

以下代码提交空用户代理.我错过了什么?

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class TestHTTP {

        public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpGet request = new HttpGet("http://tool.keepmeapi.com/echo");

        HttpContext HTTP_CONTEXT = new BasicHttpContext();
        HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
        request.setHeader("Referer", "http://www.google.com");

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request, HTTP_CONTEXT);

        if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 400) { …
Run Code Online (Sandbox Code Playgroud)

java user-agent apache-httpclient-4.x

41
推荐指数
3
解决办法
7万
查看次数

从基类方法调用基类重写函数

public class A {
    public void f1(String str) {
        System.out.println("A.f1(String)");
        this.f1(1, str);
    }

    public void f1(int i, String str) {
        System.out.println("A.f1(int, String)");
    }
}



public class B extends A {
    @Override
    public void f1(String str) {
        System.out.println("B.f1(String)");
        super.f1(str);
    }

    @Override
    public void f1(int i, String str) {
        System.out.println("B.f1(int, String)");
        super.f1(i, str);
    }
}


public class Main {
    public static void main(String[] args) {
        B b = new B();
        b.f1("Hello");
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在寻求这段代码输出:

B.f1(String)
A.f1(String)
A.f1(int, String)
Run Code Online (Sandbox Code Playgroud)

但我得到了:

B.f1(String)
A.f1(String)
B.f1(int, …
Run Code Online (Sandbox Code Playgroud)

java theory oop overloading

36
推荐指数
3
解决办法
3万
查看次数

自引用表(Oracle)上的SQL递归查询

让我们假设我有这样的样本数据:

| Name     | ID | PARENT_ID |
-----------------------------
| a1       | 1  | null      |
| b2       | 2  | null      |
| c3       | 3  | null      |
| a1.d4    | 4  | 1         |
| a1.e5    | 5  | 1         |
| a1.d4.f6 | 6  | 4         |
| a1.d4.g7 | 7  | 4         |
| a1.e5.h8 | 8  | 5         |
| a2.i9    | 9  | 2         |
| a2.i9.j10| 10 | 9         |
Run Code Online (Sandbox Code Playgroud)

我想从accountId = …

sql database oracle plsql hierarchical-data

32
推荐指数
3
解决办法
10万
查看次数

在Joda-Time中,将DateTime设置为月初

我的API允许库客户端传递日期:

method(java.util.Date date)
Run Code Online (Sandbox Code Playgroud)

Joda-Time合作,从这一天开始,我想提取月份并迭代本月所有日子.

现在,传递的日期通常是新的Date() - 意味着当前的瞬间.我的问题实际上是将新的DateMidnight(jdkDate)实例设置为月初.

有人可以用Joda-Time演示这个用例吗?

java date jodatime

31
推荐指数
4
解决办法
3万
查看次数

如何计算R中的组合和排列?

我刚开始学习概率,我正在寻找可以在linux上工作的工具.

我找到了一些组合软件包http://rss.acs.unt.edu/Rdoc/library/Combinations/html/00Index.html但是当我尝试安装它们时,该过程失败并显示以下消息:

> install.packages("Combinations")
Installing package(s) into ‘/home/maxim/R/x86_64-pc-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
  package ‘Combinations’ is not available (for R version 2.13.1)
Run Code Online (Sandbox Code Playgroud)

combinations r

26
推荐指数
3
解决办法
11万
查看次数

现代iOS 8 NSOperation使用仍然需要@autoreleasepool吗?

我已阅读并发编程指南

在指南中,文本指出GCD调度队列定义了他们自己的@autoreleasepool池,并提到仍然建议在每个调度级别定义一个,但是对于NSOperation没有说什么,Apple提供的示例代码也没有显示@autoreleasepool结构.在NSOperation的背景下,模糊地提到@autoreleasepool的唯一地方是修订历史,

2012-07-17 - 删除了有关自动释放池使用操作的过时信息.

查看在线提供的示例代码,例如 http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues在基于NSOperations的对象的实现中使用@autoreleasepool,例如:

@implementation ImageDownloader

- (void)main {
    @autoreleasepool {
      ...
    }
}     
@end
Run Code Online (Sandbox Code Playgroud)
  1. 我应该如何实现现代NSOperation对象?
  2. Apple在2012-07-17之间提到的更新是什么?

nsoperation nsautoreleasepool ios automatic-ref-counting ios8

25
推荐指数
1
解决办法
2391
查看次数

如何从mmapped文件中读取行?

似乎mmap接口只支持readline().如果我尝试迭代对象,我会得到字符而不是完整的行.

什么是逐行读取mmap文件的"pythonic"方法?

import sys
import mmap
import os


if (len(sys.argv) > 1):
  STAT_FILE=sys.argv[1]
  print STAT_FILE
else:
  print "Need to know <statistics file name path>"
  sys.exit(1)


with open(STAT_FILE, "r") as f:
  map = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
  for line in map:
    print line # RETURNS single characters instead of whole line
Run Code Online (Sandbox Code Playgroud)

python text mmap file

21
推荐指数
2
解决办法
2万
查看次数

在Yosemite Mac OS X 10.10上缺少安装nokogiri libiconv的失败

试图安装Nokogiri我收到以下错误

Maxims-MacBook-Air:ScrapingTheApple maximveksler$ gem install nokogiri
Fetching: nokogiri-1.6.2.1.gem (100%)
Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
    ERROR: Failed to build gem native extension.

    /Users/maximveksler/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb
Building nokogiri using packaged libraries.
-----
libiconv is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You …
Run Code Online (Sandbox Code Playgroud)

gem nokogiri osx-yosemite

17
推荐指数
5
解决办法
1万
查看次数

使用{}来分割大块代码以提高代码可读性 - 良好实践?

我正在考虑使用匿名{}代码块在逻辑上区分同一方法调用中的"代码块" 的选项,这(理论上)应该提高代码的可读性.

我想知道以下哪两个代码段对你的眼睛更好?

另外,2个代码段是否编译为相同的字节码?换句话说,可以使用{}以任何方式伤害代码的性能吗?

选项1:没有{}标识的代码块

 public static String serviceMatch(HttpServletRequest servletRequest, RequestTypeEnum requestTypeEnum, ...censorsed..., RequestStatistics requestStatistics) {
  Request request;

  // We get the parser that fits the ...censorsed..., effectively transforming the HTTPReqeuest to application local "Request*" object
  RequestParser parser = RequestParserFactory.getParser(...censorsed...);

  // Populate basic parameters, the "heavy" data will be lazy loaded
  request = parser.parse(servletRequest);

  // Instead of polluting the parsers let's put it here... (unless we identify meaningful justifications for the other alternative of changing RequestParser.parse() …
Run Code Online (Sandbox Code Playgroud)

java performance coding-style code-readability

15
推荐指数
2
解决办法
1210
查看次数