天天看點

JUC-5.建立執行線程的方式

1. 建立執行線程的方式

建立執行線程一共有4種方式。分别是繼承Thread、實作接口Runnable接口、實作Callable接口、線程池。

1.1 繼承Thread

繼承Thread并重寫父類run()方法,通過start()方法啟動線程。

/**
 * 建立執行線程方式一:繼承Tread
 * @author xiaobin
 * @date 2018/3/11
 */
public class TestThread {
    public static void main(String[] args) {
        ThreadDemo threadDemo1 = new ThreadDemo("A");
        ThreadDemo threadDemo2 = new ThreadDemo("B");
        threadDemo1.start();
        threadDemo2.start();
    }
}
class ThreadDemo extends Thread {
    public ThreadDemo(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("運作線程:" + this.getName());
    }
}
           

1.2 實作Runnable接口

通過實作Runnable接口,并實作方法run()完成任務。将Runnable實作類的執行個體傳到Thread構造方法,完成任務執行。

/**
 * 建立執行線程方式二:實作Runnable
 * @author xiaobin
 * @date 2018/3/11
 */
public class TestRunnable {
    public static void main(String[] args) {
        Thread th1 = new Thread(new RunnableDemo(),"A");
        Thread th2 = new Thread(new RunnableDemo(),"B");
        th1.start();
        th2.start();
    }
}

class RunnableDemo implements Runnable {

    @Override
    public void run() {
        System.out.println("運作線程:" + Thread.currentThread().getName());
    }
}
           

1.3 實作Callable接口

相對于Runnable接口的方式,方法可以有傳回值,并且可以抛出異常。

Callable需要FutureTask實作類的支援,用于接收傳回結果。get()方法是阻塞方法,直到有傳回值。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 建立執行線程方式三:實作Callable
 * @author xiaobin
 * @date 2018/3/11
 */
public class TestCallable {
    public static void main(String[] args) {
        CallableDemo callableDemo = new CallableDemo();
        //Callable需要FutureTask實作類的支援,用于接收運算結果
        FutureTask<Integer> result = new FutureTask<>(callableDemo);
        new Thread(result).start();

        try {
            System.out.println("接收結果:" + result.get());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class CallableDemo implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("執行Callable");
        return ;
    }
}
           

1.4 線程池

JUC包提供的Excetor為線程池接口,通過excute(Runnable r)方法可以将任務交給線程池進行執行。

Java提供了Executors類,該類有四個靜态方法分别可以建立不同類型的線程池(ExecutorService)。

- Executors.newCachedThreadPool() 建立可變大小的線程池

- Executors.newFixedThreadPool(int number) 建立固定大小的線程池

- Executors.newSingleThreadPool() 建立單任務線程池

- Executors.newScheduledThreadPool(int number) 建立延遲線程池

/**
 * @author xiaobin
 * @date 2018/3/11
 */
public class TestThreadPool {
    public static void main(String[] args) {

        //建立一個可重用固定線程數的線程池
        ExecutorService executorService = Executors.newFixedThreadPool();
        Thread t1 = new ThreadDemo1();
        Runnable runnable = new RunnableDemo1();
        Callable callable = new CallableDemo1();


        try {
            executorService.execute(runnable);
            executorService.execute(t1);
            Future<String> future1 = executorService.submit(callable);
            System.out.println("傳回值:" + future1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}

class CallableDemo1 implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("運作線程callable:" + Thread.currentThread().getName());
        return "123";
    }
}

class RunnableDemo1 implements Runnable {

    @Override
    public void run() {
        System.out.println("運作線程:" + Thread.currentThread().getName());
    }
}

class ThreadDemo1 extends Thread {

    @Override
    public void run() {
        System.out.println("運作線程:" + this.getName());
    }
}
           

繼續閱讀