一、繼承Thread類
通過自定義類繼承Thread類來創建
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----" + i);
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " === main");
MyThread myThread = new MyThread();
myThread.start();
}
}
二、實現Runable接口
- 通過自定義類實現Runable接口來創建
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----" + i);
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " === main");
Thread thread = new Thread(new MyThread());
thread.start();
}
}
- 使用匿名內部類來是實現
public class ThreadDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " === main");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----" + i);
}
}
});
thread.start();
}
}
- 使用lambda表達式來創建
public class ThreadDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " === main");
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----" + i);
}
},"My Thread").start();
}
}
三、實現Callable接口
通過Callable來創建線程,可以有返回值
- 自定義類實現Collable接口
class MyThread implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName());
return "hello world";
}
}
public class ThreadDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask futureTask = new FutureTask(new MyThread());
Thread thread = new Thread(futureTask);
thread.start();
// 獲取返回值
System.out.println(futureTask.get());
}
}
- 對於同一個FutureTask,不管你通過它創建多少個線程,返回值結果都是一樣的。
- Runbale與Callable的區別
- Callable實現的方法是call(),Runnable實現的方法是run().
- Callable的任務執行後有返回值,而Runnable的任務沒有返回值
- call方法可以拋出異常,run方法不可以
- 運行Callable任務可以拿到一個Future對象,表示異步計算的結果。它提供了檢查計算是否完成的方法,以等待計算的完成,並檢索計算的結果。通過Future對象可以瞭解任務執行情況,可取消任務的執行,還可獲取執行結果。
四、線程池
newFixedThreadPool
線程的數量有自己指定
public class ThreadDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("線程 " + Thread.currentThread().getName() + " 被調用了");
}
});
}
executorService.shutdown();
}
}
newSingleThreadExecutor
只有一個線程
public class ThreadDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("線程 " + Thread.currentThread().getName() + " 被調用了");
}
});
}
executorService.shutdown();
}
}
newCachedThread
根據任務多少來自動實現線程數的增減
public class ThreadDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println("線程 " + Thread.currentThread().getName() + " 被調用了");
}
});
}
executorService.shutdown();
}
}