• 操作系统_多线程笔记(二)


    1.线程状态

    状态是针对当前线程调度的情况来描述的,因为线程是系统调度的基本单位,所以状态是属于线程的属性
    线程的六种状态:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    注意:
    1.一旦内核里的PCB消亡了,此时代码中创建的thread也就没有用了,即内核里的线程释放的时候无法保证java代码中的对象也被释放,所以就需要设定特定的状态来把thread对象来标记成"无效";
    2.PCB消亡之后,还可以调用对象的一些方法属性,只是没有办法再利用多线程来做事情了;
    3.一个线程只能start一次

    看代码举例:

     public static void main(String[] args) throws InterruptedException {
            Thread thread = new Thread(() -> {
                for (int i = 0; i < 100; i++) {
    
                    try {
                        sleep(10);//休眠10ms
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
    
    
                }
            });
            System.out.println("start之前的状态:"+thread.getState());//获取PCB还没形成之前的状态
            
            thread.start();//PCB形成,线程开始工作
            System.out.println("thread执行中的状态:"+thread.getState());//获取线程工作中的状态
            
            thread.join();//main主线程等待thread线程工作完
            System.out.println("thread执行完的状态:"+thread.getState());//获取thread线程工作完之后,thread线程的状态
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    结果:
    在这里插入图片描述

    例二:

    public static void main(String[] args) throws InterruptedException {
            Thread t = new Thread(() -> {
                for (int i = 0; i < 100; i++) {
                    // 这个循环体啥都不干, 也不 sleep
                    for (int j = 0; j < 100; j++) {
                        int a = 10;
                        a += 10;
                    }
    
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            t.start();
            for (int i = 0; i < 1000; i++) {
                System.out.println("t 执行中的状态: " + t.getState());
            }
           
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    结果:
    在这里插入图片描述
    通过这里的循环获取,就能够看到这里的交替状态了,当前获取到的状态是什么,主要看系统里的操作系统的调度,获取状态的那一瞬间是什么就是什么(正在执行或者sleep)
    拿例二举例:相比于thread在cpu上的执行时间,sleep这个时间太长了
    在这里插入图片描述
    如果想让时间更均衡,可以给线程增加更复杂的逻辑


    2.多线程在的意义是什么?

    看例子:
    计算a自增10000次,和b自增10000所用的时间; 分别用单线程和多线程来感受一下

    
        //多线程执行
        public static void concurrency() {
           Thread thread1 = new Thread(() -> {
                int a = 0;
                for (long i = 0; i < 100_0000_0000L; i++) {
                    a++;
                }
            });
            Thread thread2 = new Thread(() -> {
                int b = 0;
                for (long i = 0; i < 100_0000_0000L; i++) {
                    b++;
                }
            });
            long s1 = System.currentTimeMillis();
            thread1.start();
            thread2.start();
    
            try {
                thread1.join();
                thread2.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
    
    
            long s2 = System.currentTimeMillis();
            System.out.println("并发执行所消耗的时间:"+(s2-s1)+"ms");
        }
        
    	//单线程执行
        public static void serial() {
    
          long s1 = System.currentTimeMillis();
    
    
            long a = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                a++;
            }
            long b = 0;
            for (long i = 0; i < 100_0000_0000L; i++) {
                b++;
            }
    
            long s2 = System.currentTimeMillis();
    
            System.out.println("执行时间: " + (s2 - s1) + " ms");
        }
    
        public static void main(String[] args) throws InterruptedException {
            concurrency();//多线程执行
            serial();//单线程执行
    
        }
    
    • 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
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    结果:
    在这里插入图片描述
    可以看出单线程执行的时间比多线程慢的多!

    问题一: 但是为什么多线程的执行时间不是单线程的两倍呢?
    因为线程的调度也是会消耗时间的!,多线程可以更充分的利用CPU的资源
    问题二: 两个线程是不是同时执行?
    main线程先调用thread1.start,启动thread1开始计算时间的同时main再调用thread2.start,启动thread2的同时main线程就进入thread1.join,此时main线程阻塞等待,thread1和thread2还是继续执行的;等到thread.join返回,在进入main线程就进入thread2.join阻塞等待thread2join返回,继续执行计时操作,值得注意的是虽然thread2比thread1start的晚一点,但是thread2 完全有可能比thread1先结束,这样下来就是main在thread1.join是还是正常等待
    注意:
    main线程调用thread1.join就是main等待thread1执行完

  • 相关阅读:
    Spring 源码涉及到的重要设计模式
    mysql容器bug
    【OpenGL】五、光照
    开发思维——【小项目玩出步骤】——移动端适配和项目老大做设计
    使用 PyTorch 搭建网络 - train_py篇
    多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅
    贪心算法
    Linux学习笔记
    STM32 NVIC中断优先级管理通过结构图快速理解
    1.ROS操作之从空间编译到功能包编译
  • 原文地址:https://blog.csdn.net/weixin_64634186/article/details/128106538