• 多线程并发和进程通信模拟


    一、实验目的:

    1. 通过编写多线程并发和进程通信的模拟代码,加深对多线程编程和进程通信的理解。
    2. 学习如何使用Java中的多线程和管道流来实现并发执行和进程间通信。
    3. 掌握多线程的基本概念和使用方法,以及进程通信的实现方式。

    实验设备与实验环境:

    计算机,Java编译系统,idea,ChatGPT

    二、实验程序设计内容:

    实验分为两部分:一部分是多线程并发(Exam_2.1),另一部分是进程通信(Exam_2.2)。

    1. Exam_2.1部分:用户输入并发进程的数量和执行次数,创建对应数量的线程,每个线程执行指定次数的任务并输出执行信息。
    2. Exam_2部分:使用管道流进行进程间通信,主线程创建子线程往管道中写入数据,主线程读取子线程写入的数据并输出。

    三、实验程序设计思路及流程图

    1. 在Exm2_1方法中,用户输入并发进程的数量和执行次数,创建对应数量的线程对象,并启动这些线程。
    2. 每个线程对象是myThread类的实例,其中包含线程的ID和执行次数。线程运行时输出对应的执行信息,并通过Thread.sleep模拟执行过程。
    3. 在Exm2_2方法中,使用PipedInputStream和PipedOutputStream创建管道流,并连接起来。
    4. 创建子线程写入数据到管道中,在子线程中调用myPipe.writeToPipe方法,用户输入要发送给父进程的消息,写入管道后关闭输出流。
    5. 主线程读取管道中的数据并输出,同时等待子线程结束。通过这种方式实现了进程间的通信。
    6. 在myPipe类中,实现了写入管道的方法,用户输入消息后将消息写入管道。

    四、实验源程序及注释:

    1. package homework.os;
    2. import java.io.IOException;
    3. import java.io.PipedInputStream;
    4. import java.io.PipedOutputStream;
    5. import java.util.LinkedList;
    6. import java.util.List;
    7. import java.util.Scanner;
    8. /**
    9.  * Date:2024/3/11  8:23
    10.  * Description:TODO
    11.  *
    12.  * @author Leon
    13.  * @version 1.0
    14.  */
    15. public class exm2 {
    16.     public static void main(String[] args) {
    17.         System.out.println("Please input the experiment you want to select \n" +
    18.                 "(1-Exam_2.1(thread) | 2-Exam_2.2(pipe)");
    19.         if (new Scanner(System.in).nextInt() == 1) Exm2_1();
    20.         else Exm2_2();
    21.     }
    22.     static void Exm2_1() {
    23.         Scanner sc = new Scanner(System.in);
    24.         List tList = new LinkedList<>();
    25.         System.out.println("Please enter the number of concurrent processes");
    26.         int n = sc.nextInt();
    27.         System.out.println("Please enter the number of process executions");
    28.         int m=sc.nextInt();
    29.         for (int i = 1; i <= n; i++)
    30.             tList.add(new myThread(i,m));
    31.         for (Thread t : tList)
    32.             t.start();
    33.         for (Thread t : tList)
    34.             try {
    35.                 t.join();
    36.             } catch (InterruptedException e) {
    37.                 e.printStackTrace();
    38.             }
    39.         System.out.println("Main thread exiting");
    40.     }
    41.     static void Exm2_2() {
    42.         PipedInputStream in = new PipedInputStream();
    43.         PipedOutputStream out = new PipedOutputStream();
    44.         try {
    45.             out.connect(in);
    46.             // 创建子线程写数据到管道
    47.             Thread writerThread = new Thread(() -> {
    48.                 try {
    49.                     myPipe.writeToPipe(out);
    50.                 } catch (IOException e) {
    51.                     e.printStackTrace();
    52.                 }
    53.             });
    54.             writerThread.start();
    55.             // 读取数据从管道
    56.             byte[] buffer = new byte[1024];
    57.             int bytesRead = in.read(buffer);
    58.             String receivedMessage = new String(buffer, 0, bytesRead);
    59.             System.out.println("Received from child process: " + receivedMessage);
    60.             writerThread.join(); // 等待子线程结束
    61.         } catch (IOException | InterruptedException e) {
    62.             e.printStackTrace();
    63.         }
    64.     }
    65. }
    66. class myThread extends Thread {
    67.     private int threadId;
    68.     private int exe;
    69.     public myThread(int threadId,int exe) {
    70.         this.threadId = threadId;
    71.         this.exe=exe;
    72.     }
    73.     @Override
    74.     public void run() {
    75.         for (int i = 1; i <= exe; i++) {
    76.             System.out.println("Thread " + threadId + " is running: 第" + i + "次执行");
    77.             try {
    78.                 Thread.sleep(1000);
    79.             } catch (InterruptedException e) {
    80.                 e.printStackTrace();
    81.             }
    82.         }
    83.     }
    84. }
    85. class myPipe {
    86.     static void writeToPipe(PipedOutputStream out) throws IOException {
    87.         System.out.println("Enter the message to send to the parent process: ");
    88.         String message = new Scanner(System.in).nextLine();
    89.         out.write(message.getBytes());
    90.         out.close();
    91.     }
    92. }

    五、实验程序测试过程及解释说明

    1.多线程并发

    Please input the experiment you want to select

    (1-Exam_2.1(thread) | 2-Exam_2.2(pipe)

    1

    Please enter the number of concurrent processes

    5

    Please enter the number of process executions

    3

    2.进程通信

    Please input the experiment you want to select

    (1-Exam_2.1(thread) | 2-Exam_2.2(pipe)

    2

    Enter the message to send to the parent process:

    A1高闪来一个,秋梨膏

    六、实验程序测试过程与结果分析、

    1.多线程并发

    Thread 1 is running: 第1次执行

    Thread 5 is running: 第1次执行

    Thread 4 is running: 第1次执行

    Thread 2 is running: 第1次执行

    Thread 3 is running: 第1次执行

    Thread 2 is running: 第2次执行

    Thread 4 is running: 第2次执行

    Thread 3 is running: 第2次执行

    Thread 1 is running: 第2次执行

    Thread 5 is running: 第2次执行

    Thread 2 is running: 第3次执行

    Thread 3 is running: 第3次执行

    Thread 5 is running: 第3次执行

    Thread 4 is running: 第3次执行

    Thread 1 is running: 第3次执行

    Main thread exiting

    2.进程通信

    Received from child process: A1高闪来一个,秋梨膏

    七、理论学习与实践能力锻炼方面的个人心得体会

    通过本次实验,我深入了解了多线程并发和进程通信的实现方式。

    多线程并发能够提高程序的执行效率,同时需要注意线程间的同步和互斥问题。

    进程通信是不同进程之间进行数据交换和通信的重要方式,管道流是其中一种简单有效的实现方式。

    通过实验,我对Java中多线程编程和进程通信有了更深入的理解,为以后的程序设计和开发积累了经验。

           

    实验评价及结论:

    实验目的明确、设计内容符合要求,独立完成了操作系统多线程并发和进程通信模拟程序设计任务且源程序与注释、测试过程记录完整正确,能够很好地将课程理论运用于解决实际问题;实验报告内容完整,态度认真,总体质量优秀。

  • 相关阅读:
    基于C++实现的几何学与计算机的交叉应用(四色定理、三维凸包)
    HIVE无法启动问题
    Linux上防火墙操作
    git修改文件名称提交
    争议不断的AI绘画,靠这个成为了顶流?
    10.Linear Map transformation rules
    Redis 一个key-value存储系统
    [附源码]Python计算机毕业设计Django新能源汽车租赁
    Qt小项目2 图片查看器
    Linux CentOS7 lrzsz工具
  • 原文地址:https://blog.csdn.net/vV_Leon/article/details/138199772