我是靠谱客的博主 正直睫毛,这篇文章主要介绍Java线程的创建与线程池的简单使用,现在分享给大家,希望可以做个参考。

线程的创建与线程池的简单使用

创建线程的四种方式

1. 继承Thread
创建一个ThreadDemo类继承Thread

复制代码
1
2
3
4
5
6
7
/** * 继承Thread */ public class ThreadDemo2 extends Thread{ }

重写run方法(run方法为线程的执行逻辑)

复制代码
1
2
3
4
5
6
7
8
/** *重写run方法 */ @Override public void run() { System.out.println(threadName); }

3、添加构造方法(多线程通过有参构造函数传参)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 添加属性(线程名称) */ public String threadName; /** * 添加有参构造方法(有参时为了展示多线程时的单个线程的名称) */ public ThreadDemo2(String threadName){ this.threadName = threadName; }

4、通过main方法测试

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 重写main方法测试多线程 */ public static void main(String[] args) { //创建一个线程 1.线程进入新建状态 ThreadDemo2 threadDemoA = new ThreadDemo2("A"); ThreadDemo2 threadDemoB = new ThreadDemo2("B"); //2.调用start方法使线程进入就绪状态 threadDemoA.start(); threadDemoB.start(); }

完整测试案例代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class ThreadDemo extends Thread{ private String threadName; /** * 多线程通过有参构造函数传参 * @param threadName */ public ThreadDemo(String threadName){ this.threadName = threadName; } /** * 重写run方法 * run方法为线程的执行逻辑 * 3.就绪的线程 获取到cpu执行权限后 线程进入运行状态 */ @Override public void run() { for (int i = 0; i <50 ; i++) { System.out.println(threadName+":"+i); try { //sleep 线程进入休眠状态 阻塞状态 会释放cpu权限 Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } //当线程run方法执行完毕后 线程进入死亡状态 不可用 GC回收 } public static void main(String[] args) { //创建一个线程 1.线程进入新建状态 ThreadDemo threadDemoA = new ThreadDemo("A"); ThreadDemo threadDemoB = new ThreadDemo("B"); //2.调用start方法使线程进入就绪状态 threadDemoA.start(); threadDemoB.start(); } }

2、实现Runnable接口

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class RunnableDemo implements Runnable{ private String threadName; public RunnableDemo(String threadName){ this.threadName = threadName; } @Override public void run() { for (int i = 0; i <50 ; i++) { System.out.println(threadName+":"+i); try { //sleep 线程进入休眠状态 阻塞状态 会释放cpu权限 Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new Thread(new RunnableDemo("A")).start(); new Thread(new RunnableDemo("B")).start(); } }

3、匿名内部类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ThreadSafe { private static Integer i = 0; public static void main(String[] args) { for (int j = 0; j < 5000; j++) { //匿名内部类 new Thread(new Runnable() { @Override public void run() { //线程在执行时会为自己创建工作内存 synchronized (i){ i++; } } }).start(); new Thread(new Runnable() { @Override public void run() { synchronized (i) { i--; } } }).start(); } //线程等待与synchronized同在 才能达到预想结果 try { //让主线程等待子线程执行完毕 Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("args = " + i); } }

线程池简单案例(结合消息队列代码版)

创建线程类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cn.jk.bo.CarInfoBo; import cn.jk.service.CarGoodsService; public class CarGoodsThread implements Runnable{ private CarGoodsService carGoodsService; private CarInfoBo carInfoBo; public CarGoodsThread(CarGoodsService carGoodsService, CarInfoBo carInfoBo){ this.carGoodsService=carGoodsService; } @Override public void run() { try { carGoodsService.addCarInfo(carInfoBo); } catch (InterruptedException e) { e.printStackTrace(); } } }

通过线程池执行工具执行new出来的线程(在消息队列的监听类中)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cn.jk.bo.CarInfoBo; import cn.jk.service.CarGoodsService; import cn.jk.service.CarService; import cn.jk.threadconfig.CarGoodsThread; import com.alibaba.fastjson.JSON; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.Executor; @Component public class MessageManager { @Autowired private CarGoodsService carGoodsService; @Autowired private Executor executor; //消息队列的 监听 @RabbitListener(queues = "carGoods") public void saveCarInfo(String carInfo){ CarInfoBo carInfoBo = JSON.parseObject(carInfo, CarInfoBo.class); CarGoodsThread carGoodsThread = new CarGoodsThread(carGoodsService, carInfoBo); //向多线程分发线程任务 executor.execute(carGoodsThread); } }

线程池简单案例(结合消息队列注解版)

创建线程池配置类
SpringThreadPool.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @EnableAsync//启用spring多线程注解 public class SpringThreadPool { @Bean public Executor creatSpringThreadPool(){ //创建一个spring线程池 ThreadPoolTaskExecutor springPool = new ThreadPoolTaskExecutor(); //设置核心线程数 为100 //当前任务数少于核心线程数时,每次有新的任务 都开启新线程执行 springPool.setCorePoolSize(100); //缓冲队列 当前任务数大于核心线程数 小于缓冲队列 //新任务会放入缓冲队列中 等待空闲线程 执行任务 springPool.setQueueCapacity(800000); //当前任务数大于缓冲队列 开启最大线程数 springPool.setMaxPoolSize(200); //当前任务数大于缓冲队列 并大于最大线程 走异常处理流程 //ThreadPoolExecutor.CallerRunsPolicy 让主线程代理子线程处理任务 springPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return springPool; } }

在需要用到多线程的方法上加上

复制代码
1
2
@Async//开启异步处理 把当前方法放入多线程 run方法中运行

示例代码:

复制代码
1
2
3
4
5
6
7
8
@Async//开启异步处理 把当前方法放入多线程 run方法中运行 @Override public void addUser(UserDto userDto) throws InterruptedException { //userMapper.addUser(userDto); System.out.println("userDto = " + userDto); Thread.sleep(500); }

最后

以上就是正直睫毛最近收集整理的关于Java线程的创建与线程池的简单使用的全部内容,更多相关Java线程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部