死磕 java线程系列之创建线程的8种方式
问题
简介
继承Thread类并重写run()方法public class CreatingThread01 extends Thread { @Override public void run() { System.out.println(getName() + " is running"); } public static void main(String[] args) { new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); new CreatingThread01().start(); }}
实现Runnable接口public class CreatingThread02 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } public static void main(String[] args) { new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); new Thread(new CreatingThread02()).start(); }}
匿名内部类public class CreatingThread03 { public static void main(String[] args) { // Thread匿名类,重写Thread的run()方法 new Thread() { @Override public void run() { System.out.println(getName() + " is running"); } }.start(); // Runnable匿名类,实现其run()方法 new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } }).start(); // 同上,使用lambda表达式函数式编程 new Thread(()->{ System.out.println(Thread.currentThread().getName() + " is running"); }).start(); }}
实现Callabe接口public class CreatingThread04 implements Callable<Long> { @Override public Long call() throws Exception { Thread.sleep(2000); System.out.println(Thread.currentThread().getId() + " is running"); return Thread.currentThread().getId(); } public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<Long> task = new FutureTask<>(new CreatingThread04()); new Thread(task).start(); System.out.println("等待完成任务"); Long result = task.get(); System.out.println("任务结果:" + result); }}
定时器(java.util.Timer)public class CreatingThread05 { public static void main(String[] args) { Timer timer = new Timer(); // 每隔1秒执行一次 timer.schedule(new TimerTask() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } }, 0 , 1000); }}
线程池public class CreatingThread06 { public static void main(String[] args) { ExecutorService threadPool = Executors.newFixedThreadPool(5); for (int i = 0; i < 100; i++) { threadPool.execute(()-> System.out.println(Thread.currentThread().getName() + " is running")); } }}
并行计算(Java8+)public class CreatingThread07 { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // 串行,打印结果为12345 list.stream().forEach(System.out::print); System.out.println(); // 并行,打印结果随机,比如35214 list.parallelStream().forEach(System.out::print); }}
Spring异步方法
@SpringBootApplication@EnableAsyncpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@Servicepublic class CreatingThread08Service { @Async public void call() { System.out.println(Thread.currentThread().getName() + " is running"); }}
@RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class)public class CreatingThread08Test { @Autowired private CreatingThread08Service creatingThread08Service; @Test public void test() { creatingThread08Service.call(); creatingThread08Service.call(); creatingThread08Service.call(); creatingThread08Service.call(); }}
task-3 is runningtask-2 is runningtask-1 is runningtask-4 is running
总结
彩蛋
public class CreatingThread09 { public static void main(String[] args) { new Thread(()-> { System.out.println("Runnable: " + Thread.currentThread().getName()); }) { @Override public void run() { System.out.println("Thread: " + getName()); } }.start(); }}
public class Thread implements Runnable { // Thread维护了一个Runnable的实例 private Runnable target; public Thread() { init(null, null, "Thread-" + nextThreadNum(), 0); } public Thread(Runnable target) { init(null, target, "Thread-" + nextThreadNum(), 0); } private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) { // ... // 构造方法传进来的Runnable会赋值给target this.target = target; // ... } @Override public void run() { // Thread默认的run()方法,如果target不为空,会执行target的run()方法 if (target != null) { target.run(); } }}

浙公网安备 33010602011771号