高并发编程JUC之进程与线程高并发编程JUC之进程与线程

忘是亡心i 2024-03-26 18:00 278阅读 0赞

1.准备

pom.xml 依赖如下:

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. </properties>
  6. <dependencies>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.11</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  14. <dependency>
  15. <groupId>org.projectlombok</groupId>
  16. <artifactId>lombok</artifactId>
  17. <version>1.18.22</version>
  18. <scope>provided</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.slf4j</groupId>
  22. <artifactId>slf4j-api</artifactId>
  23. <version>1.7.22</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>ch.qos.logback</groupId>
  27. <artifactId>logback-classic</artifactId>
  28. <version>1.2.3</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.junit.jupiter</groupId>
  32. <artifactId>junit-jupiter</artifactId>
  33. <version>RELEASE</version>
  34. <scope>compile</scope>
  35. </dependency>
  36. </dependencies>

logback.xml 配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true">
  3. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  4. <encoder>
  5. <pattern>%date{HH:mm:ss} [%t] %logger - %m%n</pattern>
  6. </encoder>
  7. </appender>
  8. <logger name="c" level="debug" additivity="false">
  9. <appender-ref ref="STDOUT"/>
  10. </logger>
  11. <root level="ERROR">
  12. <appender-ref ref="STDOUT"/>
  13. </root>
  14. </configuration>

2.进程与线程

2.1 进程与线程

进程

  • 程序由指令和数据组成,但这些指令要运行,数据要读写,就必须将指令加载至 CPU,数据加载至内存。在 指令运行过程中还需要用到磁盘、网络等设备。进程就是用来加载指令、管理内存、管理 IO 的 。

  • 当一个程序被运行,从磁盘加载这个程序的代码至内存,这时就开启了一个进程。

  • 进程就可以视为程序的一个实例。大部分程序可以同时运行多个实例进程(例如记事本、画图、浏览器 等),也有的程序只能启动一个实例进程(例如网易云音乐、360 安全卫士等)

线程

  • 一个进程之内可以分为一到多个线程。

  • 一个线程就是一个指令流,将指令流中的一条条指令以一定的顺序交给 CPU 执行

  • Java 中,线程作为最小调度单位,进程作为资源分配的最小单位。 在 windows 中进程是不活动的,只是作 为线程的容器

二者对比

  • 进程基本上相互独立的,而线程存在于进程内,是进程的一个子集

  • 进程拥有共享的资源,如内存空间等,供其内部的线程共享

  • 进程间通信较为复杂

    • 同一台计算机的进程通信称为 IPC(Inter-process communication)
    • 不同计算机之间的进程通信,需要通过网络,并遵守共同的协议,例如 HTTP
  • 线程通信相对简单,因为它们共享进程内的内存,一个例子是多个线程可以访问同一个共享变量

  • 线程更轻量,线程上下文切换成本一般上要比进程上下文切换低

2.2 并行与并发

单核cpu下,线程实际还是串行执行的。操作系统中有一个组件叫做任务调度器,将 cpu 的时间片(windows 下时间片最小约为 15 毫秒)分给不同的程序使用,只是由于 cpu 在线程间(时间片很短)的切换非常快,人类感觉是同时运行的 。总结为一句话就是: 微观串行,宏观并行

一般会将这种线程轮流使用 CPU 的做法称为并发, concurrent




















CPU 时间片 1 时间片 2 时间片 3 时间片 4
core 线程 1 线程 2 线程 3 线程 4

多核 cpu下,每个 核(core) 都可以调度运行线程,这时候线程可以是并行的。



























CPU 时间片 1 时间片 2 时间片 3 时间片 4
core1 线程 1 线程 2 线程 3 线程 4
core2 线程 4 线程 4 线程 2 线程 2

引用 Rob Pike 的一段描述:

并发(concurrent)是同一时间应对(dealing with)多件事情的能力 。

并行(parallel)是同一时间动手做(doing)多件事情的能力。

2.3 应用

$\textcolor{Green}{*应用之异步调用(案例1)} $

需要等待结果

这时既可以使用同步处理,也可以使用异步来处理

join 实现(同步)

  1. static int result = 0;
  2. private static void test1() throws InterruptedException {
  3. log.debug("开始");
  4. Thread t1 = new Thread(() -> {
  5. log.debug("开始");
  6. sleep(1);
  7. log.debug("结束");
  8. result = 10;
  9. }, "t1");
  10. t1.start();
  11. t1.join();
  12. log.debug("结果为:{}", result);
  13. }

输出

  1. 20:30:40.453 [main] c.TestJoin - 开始
  2. 20:30:40.541 [Thread-0] c.TestJoin - 开始
  3. 20:30:41.543 [Thread-0] c.TestJoin - 结束
  4. 20:30:41.551 [main] c.TestJoin - 结果为:10

评价

  • 需要外部共享变量,不符合面向对象封装的思想

  • 必须等待线程结束,不能配合线程池使用

Future 实现(同步)
  1. private static void test2() throws InterruptedException, ExecutionException {
  2. log.debug("开始");
  3. FutureTask<Integer> result = new FutureTask<>(() -> {
  4. log.debug("开始");
  5. sleep(1);
  6. log.debug("结束");
  7. return 10;
  8. });
  9. new Thread(result, "t1").start();
  10. log.debug("结果为:{}", result.get());
  11. }

输出

  1. 10:11:57.880 c.TestSync [main] - 开始
  2. 10:11:57.942 c.TestSync [t1] - 开始
  3. 10:11:58.943 c.TestSync [t1] - 结束
  4. 10:11:58.943 c.TestSync [main] - 结果为:10

评价

  • 规避了使用 join 之前的缺点

  • 可以方便配合线程池使用

    private static void test3() throws InterruptedException, ExecutionException {

    1. ExecutorService service = Executors.newFixedThreadPool(1);
    2. log.debug("开始");
    3. Future<Integer> result = service.submit(() -> {
    4. log.debug("开始");
    5. sleep(1);
    6. log.debug("结束");
    7. return 10;
    8. });
    9. log.debug("结果为:{}, result 的类型:{}", result.get(), result.getClass());
    10. service.shutdown();

    }

输出

  1. 10:17:40.090 c.TestSync [main] - 开始
  2. 10:17:40.150 c.TestSync [pool-1-thread-1] - 开始
  3. 10:17:41.151 c.TestSync [pool-1-thread-1] - 结束
  4. 10:17:41.151 c.TestSync [main] - 结果为:10, result 的类型:class java.util.concurrent.FutureTask

评价

  • 仍然是 main 线程接收结果

  • get 方法是让调用线程同步等待

自定义实现(同步)

见模式篇:保护性暂停模式

CompletableFuture 实现(异步)
  1. private static void test4() {
  2. // 进行计算的线程池
  3. ExecutorService computeService = Executors.newFixedThreadPool(1);
  4. // 接收结果的线程池
  5. ExecutorService resultService = Executors.newFixedThreadPool(1);
  6. log.debug("开始");
  7. CompletableFuture.supplyAsync(() -> {
  8. log.debug("开始");
  9. sleep(1);
  10. log.debug("结束");
  11. return 10;
  12. }, computeService).thenAcceptAsync((result) -> {
  13. log.debug("结果为:{}", result);
  14. }, resultService);
  15. }

输出

  1. 10:36:28.114 c.TestSync [main] - 开始
  2. 10:36:28.164 c.TestSync [pool-1-thread-1] - 开始
  3. 10:36:29.165 c.TestSync [pool-1-thread-1] - 结束
  4. 10:36:29.165 c.TestSync [pool-2-thread-1] - 结果为:10

评价

  • 可以让调用线程异步处理结果,实际是其他线程去同步等待

  • 可以方便地分离不同职责的线程池

  • 以任务为中心,而不是以线程为中心

BlockingQueue 实现(异步)
  1. private static void test6() {
  2. ExecutorService consumer = Executors.newFixedThreadPool(1);
  3. ExecutorService producer = Executors.newFixedThreadPool(1);
  4. BlockingQueue<Integer> queue = new SynchronousQueue<>();
  5. log.debug("开始");
  6. producer.submit(() -> {
  7. log.debug("开始");
  8. sleep(1);
  9. log.debug("结束");
  10. try {
  11. queue.put(10);
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. }
  15. });
  16. consumer.submit(() -> {
  17. try {
  18. Integer result = queue.take();
  19. log.debug("结果为:{}", result);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. });
  24. }
不需等待结果

这时最好是使用异步来处理

普通线程实现
  1. @Slf4j(topic = "c.FileReader")
  2. public class FileReader {
  3. public static void read(String filename) {
  4. int idx = filename.lastIndexOf(File.separator);
  5. String shortName = filename.substring(idx + 1);
  6. try (FileInputStream in = new FileInputStream(filename)) {
  7. long start = System.currentTimeMillis();
  8. log.debug("read [{}] start ...", shortName);
  9. byte[] buf = new byte[1024];
  10. int n = -1;
  11. do {
  12. n = in.read(buf);
  13. } while (n != -1);
  14. long end = System.currentTimeMillis();
  15. log.debug("read [{}] end ... cost: {} ms", shortName, end - start);
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }

没有用线程时,方法的调用是同步的:

  1. @Slf4j(topic = "c.Sync")
  2. public class Sync {
  3. public static void main(String[] args) {
  4. String fullPath = "E:\1.mp4";
  5. FileReader.read(fullPath);
  6. log.debug("do other things ...");
  7. }
  8. }

输出

  1. 18:39:15 [main] c.FileReader - read [1.mp4] start ...
  2. 18:39:19 [main] c.FileReader - read [1.mp4] end ... cost: 4090 ms
  3. 18:39:19 [main] c.Sync - do other things ...

使用了线程后,方法的调用时异步的:

  1. private static void test1() {
  2. new Thread(() -> FileReader.read(Constants.MP4_FULL_PATH)).start();
  3. log.debug("do other things ...");
  4. }

输出

  1. 18:41:53 [main] c.Async - do other things ...
  2. 18:41:53 [Thread-0] c.FileReader - read [1.mp4] start ...
  3. 18:41:57 [Thread-0] c.FileReader - read [1.mp4] end ... cost: 4197 ms
线程池实现
  1. private static void test2() {
  2. ExecutorService service = Executors.newFixedThreadPool(1);
  3. service.execute(() -> FileReader.read(Constants.MP4_FULL_PATH));
  4. log.debug("do other things ...");
  5. service.shutdown();
  6. }

输出

  1. 11:03:31.245 c.TestAsyc [main] - do other things ...
  2. 11:03:31.245 c.FileReader [pool-1-thread-1] - read [1.mp4] start ...
  3. 11:03:33.479 c.FileReader [pool-1-thread-1] - read [1.mp4] end ... cost: 2235 ms
CompletableFuture 实现
  1. private static void test3() throws IOException {
  2. CompletableFuture.runAsync(() -> FileReader.read(Constants.MP4_FULL_PATH));
  3. log.debug("do other things ...");
  4. System.in.read();
  5. }

输出

  1. 11:09:38.145 c.TestAsyc [main] - do other things ...
  2. 11:09:38.145 c.FileReader [ForkJoinPool.commonPool-worker-1] - read [1.mp4] start ...
  3. 11:09:40.514 c.FileReader [ForkJoinPool.commonPool-worker-1] - read [1.mp4] end ... cost: 2369 ms

以调用方角度来讲,

  • 如果 需要等待结果返回,才能继续运行就是同步

  • 不需要等待结果返回,就能继续运行就是异步

1.设计

多线程可以让方法执行变为异步的(即不要巴巴干等着)、比如说读取磁盘文件时,假设读取操作花费了 5 秒钟,如果没有线程调度机制,这 5 秒 cpu 什么都做不了,其它代码都得暂停…

2.结论

  • 比如在项目中,视频文件需要转换格式等操作比较费时,这时开一个新线程处理视频转换,避免阻塞主线程

  • tomcat 的异步 servlet 也是类似的目的,让用户线程处理耗时较长的操作,避免阻塞 tomcat 的工作线程

  • ui 程序中,开线程进行其他操作,避免阻塞 ui 线程

![Image 1][]

充分利用多核 cpu 的优势,提高运行效率。想象下面的场景,执行 3 个计算,最后将计算结果汇总。

  1. 计算 1 花费 10 ms
  2. 计算 2 花费 11 ms
  3. 计算 3 花费 9 ms
  4. 汇总需要 1 ms
  • 如果是串行执行,那么总共花费的时间是 10 + 11 + 9 + 1 = 31ms

  • 但如果是四核 cpu,各个核心分别使用线程 1 执行计算 1,线程 2 执行计算 2,线程 3 执行计算 3,那么 3 个 线程是并行的,花费时间只取决于最长的那个线程运行的时间,即 11ms 最后加上汇总时间只会花费 12ms

注意:

需要在多核 cpu 才能提高效率,单核仍然时是轮流执行

1.设计

代码见【应用之效率-案例1】

2.结论
  1. 单核 cpu 下,多线程不能实际提高程序运行效率,只是为了能够在不同的任务之间切换,不同线程轮流使用 cpu ,不至于一个线程总占用 cpu,别的线程没法干活

  2. 多核 cpu 可以并行跑多个线程,但能否提高程序运行效率还是要分情况的

      • 有些任务,经过精心设计,将任务拆分,并行执行,当然可以提高程序的运行效率。但不是所有计算任 务都能拆分(参考后文的【阿姆达尔定律】)
      • 也不是所有任务都需要拆分,任务的目的如果不同,谈拆分和效率没啥意义
  3. IO 操作不占用 cpu,只是我们一般拷贝文件使用的是【阻塞 IO】,这时相当于线程虽然不用 cpu,但需要一 直等待 IO 结束,没能充分利用线程。所以才有后面的【非阻塞 IO】和【异步 IO】优化。

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 并发编程线进程

    进程与线程的并发 在讲并发之前,我们需要了解几个概念:什么是串行、并发、并行? 串行:完整执行完一个程序再执行下一个 并发:程序之间看起来是同时运行的