package com.yyc.security;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @Auther: yangyongcui
* @Date: 2018/8/22: 11:15
* @Description:
*/
public class ThreadTest implements Runnable {
private String threadName;
ThreadTest(String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
System.out.println(threadName + "执行中...");
}
public static void main(String[] args) throws InterruptedException {
ThreadTest t1 = new ThreadTest("t1");
Thread thread1 = new Thread(t1);
thread1.start();
thread1.join();
ThreadTest t2 = new ThreadTest("t2");
Thread thread2 = new Thread(t2);
thread2.start();
thread2.join();
ThreadTest t3 = new ThreadTest("t3");
Thread thread3 = new Thread(t3);
thread3.start();
thread3.join();
//=================================第二种=====================================
System.out.println("=============================================");
ExecutorService executorService=Executors.newSingleThreadExecutor();
executorService.submit(t1);
executorService.submit(t2);
executorService.submit(t3);
executorService.shutdown();
}
}
如何保证线程线性执行
最新推荐文章于 2021-09-06 23:09:57 发布
本文通过具体示例介绍了如何使用Java创建线程并利用ExecutorService进行线程池管理,展示了两种不同的线程执行方式。
937

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



