Java多线程实战
java多线程(超详细)
java自定义线程池总结
Java创建线程方式
-
方法1,继承Thread类
-
方法2,实现Runable接口
-
方法2-2,匿名内部类形式+lambda表达式
-
方法3,实现Callable接口,允许有返回值
-
方法4,线程池方式
常见线程池:
public class ThreadTest {public static void main(String[] args) {// 方法一MyThread myThread = new MyThread();myThread.start();// 方法2MyRunable myRunable = new MyRunable();Thread thread = new Thread(myRunable);thread.start();// 方法2-2Thread thread1 = new Thread(() -> {try {Thread.sleep(5);System.out.println(Thread.currentThread().getName() + "匿名线程形式运行中");Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}});thread1.start();// 方法3MyCallable myCallable = new MyCallable();FutureTask futureTask = new FutureTask<>(myCallable);Thread thread2 = new Thread(futureTask);thread2.start();try {Object o = futureTask.get();System.out.println(o);} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}// 方法4-线程池,常有线程池ExecutorService threadPool = Executors.newFixedThreadPool(10);threadPool.execute(myThread);threadPool.execute(myRunable);Future<?> ret = threadPool.submit(() -> {try {Thread.sleep(5);System.out.println(Thread.currentThread().getName() + "Callable形式运行中");Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}return "yangshun";});try {System.out.println(ret.get());} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}threadPool.shutdown();// 方法4-2:自定义线程池ThreadPoolExecutor pool = new ThreadPoolExecutor(1, //coreSize2, //MaxSize60, //60TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(3), //指定一种队列 (有界队列),new ThreadPoolExecutor.CallerRunsPolicy() //自定义拒绝策略//, new DiscardOldestPolicy() //可以使用默认拒绝策略);ThreadPoolExecutor executor = new ThreadPoolExecutor(// 核心线程数3,// 最大线程数5,// 空闲线程最大存活时间60L,// 空闲线程最大存活时间单位TimeUnit.SECONDS,// 等待队列及大小new ArrayBlockingQueue<>(100),// 创建新线程时使用的工厂Executors.defaultThreadFactory(),// 当线程池达到最大时的处理策略
// new ThreadPoolExecutor.AbortPolicy() // 抛出RejectedExecutionHandler异常new ThreadPoolExecutor.CallerRunsPolicy() // 交由调用者的线程执行
// new ThreadPoolExecutor.DiscardOldestPolicy() // 丢掉最早未处理的任务
// new ThreadPoolExecutor.DiscardPolicy() // 丢掉新提交的任务);// 总共5个任务for (int i = 1; i <= 5; i++) {int taskIndex = i;executor.execute(() -> {System.out.println("线程 " + Thread.currentThread().getName() + " 正在执行任务 " + taskIndex);// 每个任务耗时1秒try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}});}executor.shutdown();System.out.println("主线程运行结束");}@Testpublic void test() {ExecutorService threadPool = Executors.newCachedThreadPool();for(int i = 0; i < 100; i++) {try {Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}threadPool.execute(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + "正在执行中。。。");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});}threadPool.shutdown();}}
class MyCallable implements Callable{@Overridepublic Object call() throws Exception {try {Thread.sleep(5);System.out.println(Thread.currentThread().getName() + "Callable形式运行中");Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}return "运行成功!yes";}
}
class MyThread extends Thread {@Overridepublic void run() {try {Thread.sleep(5);System.out.println(Thread.currentThread().getName() + "运行中");Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}class MyRunable implements Runnable {@Overridepublic void run() {try {Thread.sleep(5);System.out.println(Thread.currentThread().getName() + "Runable形式运行中");Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
SpringBoot创建线程方式
【并发编程】SpringBoot创建线程池的六种方式
java并发学习–第二章 spring boot实现线程的创建
CompletableFuture使用
Java多线程(十): FutureTask CompletableFuture详解