在面试过程中,我们会经常遇到的一个问题:Java中创建一个线程的方式有几种?
这个问题,站在不同的角度来看,答案会有所不同。
一、从本质出发
如果站在创建线程最本质的方式,我认为其实只有一种,就是构造Thread类。
构造Thread,又衍生出两种方式,一是继承Thread类,二是实现Runnable接口,但无论是那种方式,最终也要创建Thread类的对象或其子类对象。
继承Thread方式:
package com.xiao.createthreads;
public class ThreadStyle extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getState());
}
public static void main(String[] args) {
ThreadStyle threadStyle = new ThreadStyle();
System.out.println(threadStyle.getState());
threadStyle.start();
}
}
实现Runnable接口的方式:
package com.xiao.createthreads;
public class RunnableStyle implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().getState());
}
public static void main(String[] args) {
Thread thread = new Thread(new RunnableStyle()); // 创建线程
System.out.println(thread.getState());
thread.start(); // 启动线程
}
}
二、从代码的实现方面考虑
从代码的具体实现方面考虑,那创建线程的方式就有很多种了,如使用线程池(Executors)、定时器(Timer)等等。
线程池方式:
package com.xiao.createthreads.wrongways;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolStyle {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 1000; i++) {
executorService.submit(new Task());
}
executorService.shutdown();
}
}
class Task implements Runnable{
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
定时器方式:
package com.xiao.createthreads.wrongways;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTaskStyle {
public static void main(String[] args) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}, 1000, 1000);
}
}
以上两种方式,虽然也创建了线程,但本质上创建线程的方式并没有变化,只是Java帮我们进行了一系列的封装罢了。
如使用定时器的方式,跟进源码中可以发现,他实际上创建了一个TimerThread类的对象,而该类继承自Thread类。
当然,还有其他更多创建线程方式的变种,就需要小伙伴们自行发觉了!啊哈哈哈~