- 被问及 Java 多线程,多会想到 Thread, Runnable,更通常是用 new Thread(){public void run(){...}}.start() 来启动一个线程。那都是 JDK 1.5 之前的年代了,现在还这么回答就 Out 了。用用 JDK 1.5 给我们带来的 java.util.concurrent 吧,更酷了。这里不涉及它的并发集合类,同步互斥机制,只说线程及线程池的应用举例。
1. 新的启动线程的方式:Read More1public static void main(String[] args) throws Exception { 2 Callable<Integer> callable = new Callable<Integer>() { 3 public Integer call() throws Exception { 4 System.out.println("callable executed."); 5 return new Random().nextInt(100); 6 } 7 }; 8 9 FutureTask<Integer> future = new FutureTask<Integer>(callable); 10 new Thread(future).start(); 11 12 System.out.println("do your things here"); 13 14 System.out.println(future.get()); 15}