0、繼承Thread
class MyThread extends Thread{
public void run(){
//業務邏輯
}
}
Thread temp = new MyThread();
temp.start();
1、實現Runnable
class MyTask implements Runnable{
public void run(){
//業務邏輯
}
}
Thread myThread = new Thread(new MyTask());
myThread.start();
2、實現Callable(需要藉助FutureTask、Thread)
class Temp<T> implements Callable<T>{
public T call() throws Exception {
return null;
}
}
//此處省略好多……
3、ExecutorService、Callable、Future(Executor框架裏面嘛,其實底層還是Thread,線程池要醉了)
參考文章:JAVA多線程實現的四種方式 https://www.cnblogs.com/felixzh/p/6036074.html