以前建立執行緒有兩種方式:一種是繼承Thread類,重寫run()方法;另一種就是實現runnable介面的run()方法。我們先來看看這兩種方式的實現。  

1、繼承Thread類
package com.jdk8.Thread;

public class ThreadDemo extends Thread{

    @Override
    public void run() {
        for(int i = 0 ; i < 5 ; i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();
        td.start();
        System.out.println("main thread");
    }
}

執行結果:

main thread
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0

2、實現Runnable介面

package com.jdk8.Thread;

public class RunnableDemo implements Runnable {

    public static void main(String[] args) {
        new Thread(new RunnableDemo()).start();
        System.out.println("main thread");
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        }
    }
}

執行結果:

main thread
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0

以上兩種建立執行緒的方式,run()方法都是void,不能返回值。
Callable介面自帶泛型,call()方法的返回值要跟介面的泛型一致

3、實現Callable介面,結合Future

package com.jdk8.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDemo implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
        return "over";
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        Future<String> f = pool.submit(new CallableDemo());
        try {
            System.out.println(f.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            pool.shutdown();
        }
        System.out.println("main thread");
    }
}

執行結果:

pool-1-thread-1:0
pool-1-thread-1:1
pool-1-thread-1:2
pool-1-thread-1:3
pool-1-thread-1:4
over
main thread

從上面的結果可以看出,f.get()方法可以獲取到執行緒的返回結果,並且會阻塞當前執行緒,直到執行緒執行完拿到返回結果之後,main執行緒才繼續執行。

.