1继承Thread
public class TestThreadCreate extends Thread{
@Override
public void run() {
super.run();
}
}2实现Runnable接口
public class TestThreadCreate implements Runnable{
@Override
public void run() {
super.run();
}
}3匿名内部类
new Thread(new Runnable() {
@Override
public void run() {
}
});4实现Callable
public class TestThreadCreate implements Callable<Integer>{
@Override
public Integer call() throws Exception {
return null;
}
}5定时器
public class TestThreadCreate extends TimerTask{
@Override
public void run() {
System.out.println("--------");
}
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask timerTask = new TestThreadCreate();
timer.schedule(timerTask,0,3000);
}
}6线程池的实现
public class TestThreadCreate {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.execute(()->{
System.out.println("-----------");
});
}
}7Lambad
new Thread(() -> {
}).start();8spring实现
在方法上加上@Async
Java线程创建方式总结
本文总结了Java中创建线程的多种方式,包括继承Thread类、实现Runnable接口、使用匿名内部类、实现Callable接口、利用定时器、通过线程池以及采用Lambda表达式等方法。

被折叠的 条评论
为什么被折叠?



