1. 繼承 Thread 類,然後調用 start 方法。
Java
class SampleThread extends Thread { //重寫run方法,線程運行後,跑的就是run方法 public void run(){ //System.out.println(""); } public static void main(String[] args){ Thread t1 = new SampleThread(); Thread t2 = new SampleThread(); t1.start(); //線程運行,調用的 run()方法. t2.start(); //線程運行,調用的 run()方法.. } }
12345678910111213 | class SampleThread extends Thread { //重寫run方法,線程運行後,跑的就是run方法 public void run(){ //System.out.println(""); } public static void main(String[] args){ Thread t1 = new SampleThread(); Thread t2 = new SampleThread(); t1.start(); //線程運行,調用的 run()方法. t2.start(); //線程運行,調用的 run()方法.. }} |
---|
2. 實現 Runnable 接口的 run 方法, 然後再用 Thread 類包裹後,調用 start 方法。
Java
class A implements Runnable{ @Override public void run() { // implement run method here } public static void main() { final A obj = new A(); Thread t1 = new Thread(new A()); t1.start(); } }
12345678910111213141516 | class A implements Runnable{ @Override public void run() { // implement run method here } public static void main() { final A obj = new A(); Thread t1 = new Thread(new A()); t1.start(); } } |
---|
3. 實現 Callable 接口的 call 方法,用 FutureTask 類包裹 Callable 對象。然後再用 Thread 類包裹 FutureTask 類,並調用 start 方法。call() 方法可以有返回值。
Java
class MyCallable implements Callable { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } return sum; } public static void main(String[] args) throws Exception { MyCallable mc = new MyCallable(); //實例化 callable FutureTask oneTask = new FutureTask(mc); //用FutureTask包裹 Thread oneThread = new Thread(oneTask); //用Thread包裹 oneThread.start(); System.out.print(oneTask.get()); //獲取返回值 } }
12345678910111213141516171819 | class MyCallable implements Callable { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } return sum; } public static void main(String[] args) throws Exception { MyCallable mc = new MyCallable(); //實例化 callable FutureTask oneTask = new FutureTask(mc); //用FutureTask包裹 Thread oneThread = new Thread(oneTask); //用Thread包裹 oneThread.start(); System.out.print(oneTask.get()); //獲取返回值 }} |
---|
Callable 方法在 Java 8 後,支持拉姆達表達式的寫法,可以創建一個 FutureTask 類,語句上不是太羅嗦。 Callable 方式有以下幾個優點:
- 可以捕獲線程上的異常。
- 可以通過 get 方法得到返回值。
- get 方法阻塞當前線程,直到調用的線程運行結束。
- 可以取消線程的運行。
下面代碼演示了使用 FutureTask 類運行線程,捕獲異常的例子:
Java
FutureTask<Integer> task=new FutureTask<Integer>(()->{ throw new Exception("自定義異常"); }); new Thread(task).start(); try { System.out.println(task.get()); } catch (Exception e) { System.out.println(e.getMessage()); }
1234567891011 | FutureTask<Integer> task=new FutureTask<Integer>(()->{ throw new Exception("自定義異常");}); new Thread(task).start(); try { System.out.println(task.get());} catch (Exception e) { System.out.println(e.getMessage());} |
---|
Java 6 之後,還可以通過創建線程池來創建線程,使用 ExecutorService 的 execute 方法:
Java
ExecutorService es = Executors.newCachedThreadPool(); Runnable r = <your runnable here>; es.execute(r);
123 | ExecutorService es = Executors.newCachedThreadPool();Runnable r = <your runnable here>;es.execute(r); |
---|