创建线程的几种方式
代码结构
源码
package com.myspringboot.juc.c01thread;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
public class CreateThread {
public static void main(String[] args) {
// 继承Thread类
new MyThread().start();
// 实现Runnable接口
new Thread(new MyRunnable()).start();
// 实现Callable接口
Executors.newCachedThreadPool().submit(new MyCallable());
// Lambda表达式
new Thread(() -> System.out.println("Lambda!")).start();
}
static class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread!");
}
}
static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable!");
}
}
static class MyCallable implements Callable<String> {
@Override
public String call() {
System.out.println("Callable!");
return "Callable!";
}
}
}