分类 计算机相关 下的文章

newCachedThreadPool 创建一个线程池,根据需要创建线程。如果有线程可用(空闲中),它将被重新使用。这个线程池中的线程是短暂的。六十秒内没有被使用的线程会被终止并从缓存中删除。这种线程池对于执行许多短暂的异步任务很有用。这些线程池被用来提高程序的性能。

In general, the ExecutorService will not be automatically destroyed when there is no task to process. It will stay alive and wait for new work to do.

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Main {

  public static void main(String args[]) {

    Runnable task = new Runnable() {
      @Override
      public void run() {
        System.out.println("Task is executed by : "
            + Thread.currentThread().getName());
      }
    };

    Thread t = new Thread(task, "MY_THREAD");
    t.start();

    Executor e = Executors.newSingleThreadExecutor();
    e.execute(task);

  }
}

抽象方法和抽象类看上去是多余的,对于抽象方法,不知道如何实现,定义一个空方法体不就行了吗?而抽象类不让创建对象,看上去只是增加了一个不必要的限制。

如果一个类只涉及到方法的处理,不涉及属性变量,那么这个类中的方式是否都可以写成 static,例如:

@PropertySource注解

@PropertySource 注解是 Spring 3.1 开始引入的配置类注解。通过 @PropertySource 注解将 properties 配置文件中的值存储到 Spring 的 Environment中,Environment 接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。也可以使用@Value注解用${}占位符注入属性。

一些 API 的报错信息通过 Response 的 body 返回。使用 HttpClient 能正常获取到 StatusCode 和 body 中的错误提示。然而使用 RestTemplate ,会直接抛出下面的异常。如果想获取原始的信息并进一步处理会比较麻烦。