zoukankan      html  css  js  c++  java
  • 多线程编程学习

    前言

    线程中会出现的很多问题:

    1. 所见非所得
    2. 无法肉眼去检测程序的准确性
    3. 不同平台会有不同的表现
    4. 错误很难重现

    正因为这些问题,学习多线程会有比较打的困难。

    内存模型来解决不同线程之间对同一个变量的读写同步问题


     目录

    • 线程状态
    • 线程终止(stop、interrupt、标志位)
    • 线程协调(wait/notify机制)(pack/unpack机制)伪唤醒
    • 线程池使用 (核心线程、队列、无界队列、延时队列)

     线程状态 

    1. New:尚未启动的线程状态
    2. Runnable:可运行的线程状态,等待cpu资源的调用
    3. Blocked:线程阻塞等待监视器锁定的线程状态
    4. Waiting:等待线程的线程状态。  不带超时的方法:Object.wait、Thread.join、LockSupport.park
    5. Timed Waiting:具有指定等待时间的等待线程的线程状态。   带超时的方法:Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil
    6. Terminated:终止线程的线程状态。线程正常完后或者异常终止

                  

            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("thread1执行的方法。。。");
                }
            });
            thread1.start();
    线程的创建和运行
            Thread thread2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {// 将线程2移动到等待状态,1500后自动唤醒
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread2当前状态:" + Thread.currentThread().getState().toString());
                    System.out.println("thread2 执行了");
                }
            });
            thread2.start();
    线程等待(sleep方式)
            Thread thread3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (Test.class) {
                        System.out.println("thread3当前状态:" + Thread.currentThread().getState().toString());
                        System.out.println("thread3 执行了");
                    }
                }
            });
            synchronized (Test.class) {
                System.out.println("没调用start方法,thread3当前状态:" + thread3.getState().toString()); //没调用start方法,thread3当前状态:NEW
                thread3.start();
                System.out.println("调用start方法,thread3当前状态:" + thread3.getState().toString()); //调用start方法,thread3当前状态:RUNNABLE
                Thread.sleep(200L); // 等待200毫秒,再看状态
                System.out.println("等待200毫秒,再看thread3当前状态:" + thread3.getState().toString()); //等待200毫秒,再看thread3当前状态:BLOCKED
            }
            Thread.sleep(3000L); // 再等待3秒,让thread3执行完毕,再看状态
            System.out.println("等待3秒,让thread3抢到锁,再看thread3当前状态:" + thread3.getState().toString()); //等待3秒,让thread3抢到锁,再看thread3当前状态:TERMINATED
    阻塞

      线程终止

      1、Stop:终止线程,并且清除监视器锁的信息,不会保证同步代码的一致性,破坏原子性,JDK不建议使用

    class StopThread extends Thread {
        private int i = 0, j = 0;
    
        @Override
        public void run() {
            synchronized (this) {
                // 增加同步锁,确保线程安全
                ++i;
                try {
                    // 休眠10秒,模拟耗时操作
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ++j;
            }
        }
    
        /** * 打印i和j */
        public void print() {
            System.out.println("i=" + i + " j=" + j);
        }
    }
    
    public class Demo3 {
        public static void main(String[] args) throws InterruptedException {
            StopThread thread = new StopThread();
            thread.start();
            // 休眠1秒,确保i变量自增成功
            Thread.sleep(1000);
            // 暂停线程
              thread.stop(); // 错误的终止
    
            while (thread.isAlive()) {
                // 确保线程已经终止
            } // 输出结果
            thread.print();// i=1 j=0  理想中的状态应该是  i=0 j=0 或者 i=1 j=1
        }
    }
    

      2、Interrupt:目标线程在调用wait()、wait(long)或wait(long,int)、join()、join(long,int)或sleep(long,int)方法时被阻塞,调用interrupt会生效,并且抛出InterruptedException异常,被I/O操作或者NIO中的Channel所阻塞,同样也会被中断和返回异常值。

    //正确的中断方式
    class StopThread extends Thread {
        private int i = 0, j = 0;
    
        @Override
        public void run() {
            synchronized (this) {
                // 增加同步锁,确保线程安全
                ++i;
                try {
                    // 休眠10秒,模拟耗时操作
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ++j;
            }
        }
    
        /** * 打印i和j */
        public void print() {
            System.out.println("i=" + i + " j=" + j);
        }
    }
    
    public class Demo3 {
        public static void main(String[] args) throws InterruptedException {
            StopThread thread = new StopThread();
            thread.start();
            // 休眠1秒,确保i变量自增成功
            Thread.sleep(1000);
            // 暂停线程
    //          thread.stop(); // 错误的终止
            thread.interrupt(); // 正确终止
            while (thread.isAlive()) {
                // 确保线程已经终止
            } // 输出结果
            thread.print();// i=1 j=1 正确的输出
        }
    }
    

      3、状态位:使用状态位来控制线程的终止

    public class Demo4 extends Thread {
      public volatile static boolean flag = true;
    
      public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
          try {
            while (flag) { // 判断是否运行
              System.out.println("运行中");
              Thread.sleep(1000L);
            }
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }).start();
        // 3秒之后,将状态标志改为False,代表不继续运行
        Thread.sleep(3000L);
        flag = false;
        System.out.println("程序运行结束");
      }
    }
    
    //////// 输出 //////////
    运行中
    运行中
    运行中
    程序运行结束
    

    线程协调

      1、suspend/resume  jdk弃用 ,特别容易造成死锁的问题

        public static Object baozidian = null;
    
        public void suspendResumeDeadLockTest() throws Exception {
            // 启动线程
            Thread consumerThread = new Thread(() -> {
                if (baozidian == null) { // 如果没包子,则进入等待
                    System.out.println("1、进入等待");
                    // 当前线程拿到锁,然后挂起
                    synchronized (this) {
                        Thread.currentThread().suspend();
                    }
                }
                System.out.println("2、买到包子,回家");
            });
            consumerThread.start();
            // 3秒之后,生产一个包子
            Thread.sleep(3000L);
            baozidian = new Object();
            // 争取到锁以后,再恢复consumerThread
            synchronized (this) {
                consumerThread.resume();
            }
            System.out.println("3、通知消费者");
        }
        //在程序挂起的时候,不会释放同步锁,调用resume方法的时候需要同步锁的获取,因此死锁
    

      2、wait/notify机制        (只能在同一对象锁的持有者线程调用,也就是同步代码块)

      :wait会将线程加入等待集合,并且放弃当前锁;wait会自动解锁,但是对顺序会有要求,在调用notify后,才调用wait方法,就会一直停留在WAITING状态。

        public static Object baozidian = null;
    
        /** 正常的wait/notify */
        public void waitNotifyTest() throws Exception {
            // 启动线程
            new Thread(() -> {
                synchronized (this) {
                    while (baozidian == null) { // 如果没包子,则进入等待
                        try {
                            System.out.println("1、进入等待");
                            this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("2、买到包子,回家");
            }).start();
            // 3秒之后,生产一个包子
            Thread.sleep(3000L);
            baozidian = new Object();
            synchronized (this) {
                this.notify();
                System.out.println("3、通知消费者");
            }
        }
        //在程序挂起的时候,会释放同步锁,调用notify方法的时候需要同步锁的获取,因此可以正确运行
    

      3、park/unpark机制         (调用park等待“许可”,unpark方法指定线程提供“许可”)

      :park/unpark机制,调用park不会释放锁,但是不需要按照顺序  

        public static Object baozidian = null;
    
        /** 正常的park/unpark */
        public void parkUnparkTest() throws Exception {
            // 启动线程
            Thread consumerThread = new Thread(() -> {
                while (baozidian == null) { // 如果没包子,则进入等待
                    try {
                        Thread.sleep(5000L);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    System.out.println("1、进入等待");
                    LockSupport.park();
                }
                System.out.println("2、买到包子,回家");
            });
            consumerThread.start();
            // 3秒之后,生产一个包子
            Thread.sleep(3000L);
            baozidian = new Object();
            LockSupport.unpark(consumerThread);
            System.out.println("3、通知消费者");
        }

      park/unpark机制死锁问题

        public static Object baozidian = null;
    
        /** 死锁的park/unpark */
        public void parkUnparkDeadLockTest() throws Exception {
            // 启动线程
            Thread consumerThread = new Thread(() -> {
                if (baozidian == null) { // 如果没包子,则进入等待
                    System.out.println("1、进入等待");
                    // 当前线程拿到锁,然后挂起
                    synchronized (this) {
                        LockSupport.park();
                    }
                }
                System.out.println("2、买到包子,回家");
            });
            consumerThread.start();
            // 3秒之后,生产一个包子
            Thread.sleep(3000L);
            baozidian = new Object();
            // 争取到锁以后,再恢复consumerThread
            synchronized (this) {
                LockSupport.unpark(consumerThread);
            }
            System.out.println("3、通知消费者");
        }
        //在挂其中不会释放同步锁,因此死锁
    

     线程池

      在线程中,如果线程的创建时间+销毁时间>线程任务时间,就很不合理,线程的大量堆积会造成内存的消耗,CPU频繁的切换会造成性能的消耗,因此需要线程的复用

    1. 线程池管理器:用于创建并管理线程池,包括创建线程,销毁线程,添加新任务
    2. 工作线程:可复用的工作线程
    3. 接口:任务实现的接口
    4. 任务队列:用于处理没有处理的任务,是一种缓冲机制

    线程池的实现类与接口

    ExectuorService实现方法

    ScheduledThreadPoolExector实现方法

      schedule:创建一个任务,过了延迟时间就会被执行

      scheduleAtFixedRate:创建一个周期性任务,过了延迟时间会被执行,一次任务执行时超过了周期时间,下一次任务会等到该任务结束后,立即执行

      scheduleWithFixedDelay:创建一个周期性任务,过了延迟时间会被执行,一次任务执行时超过了周期时间,下一次任务会等到该任务结束的基础上。进行延迟,再执行下一次任务

        /*模拟创建15个工作线程 */
        public void testCommon(ThreadPoolExecutor threadPoolExecutor) throws Exception {
            // 测试: 提交15个执行时间需要3秒的任务,看超过大小的2个,对应的处理情况
            for (int i = 0; i < 15; i++) {
                int n = i;
                threadPoolExecutor.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("开始执行:" + n);
                            Thread.sleep(3000L);
                            System.err.println("执行结束:" + n);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
                System.out.println("任务提交成功 :" + i);
            }
            // 查看线程数量,查看队列等待数量
            Thread.sleep(500L);
            System.out.println("当前线程池线程数量为:" + threadPoolExecutor.getPoolSize());
            System.out.println("当前线程池等待的数量为:" + threadPoolExecutor.getQueue().size());
            // 等待15秒,查看线程数量和队列数量(理论上,会被超出核心线程数量的线程自动销毁)
            Thread.sleep(15000L);
            System.out.println("当前线程池线程数量为:" + threadPoolExecutor.getPoolSize());
            System.out.println("当前线程池等待的数量为:" + threadPoolExecutor.getQueue().size());
        }
    模拟15个工作线程

     线程池创建:核心线程数量5,最大数量10,队列3,超出核心线程数量的线程存活时间:5秒

        /**
         * 2、 线程池信息: 核心线程数量5,最大数量10,队列大小3,超出核心线程数量的线程存活时间:5秒, 指定拒绝策略的
         *
         * @throws Exception
         */
        private void threadPoolExecutorTest2() throws Exception {
            // 创建一个 核心线程数量为5,最大数量为10,等待队列最大是3 的线程池,也就是最大容纳13个任务。
            // 默认的策略是抛出RejectedExecutionException异常,java.util.concurrent.ThreadPoolExecutor.AbortPolicy
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
                    new LinkedBlockingQueue<Runnable>(3), new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    System.err.println("有任务被拒绝执行了");
                }
            });
            testCommon(threadPoolExecutor);
            // 预计结果:
            // 1、 5个任务直接分配线程开始执行
            // 2、 3个任务进入等待队列
            // 3、 队列不够用,临时加开5个线程来执行任务(5秒没活干就销毁)
            // 4、 队列和线程池都满了,剩下2个任务,没资源了,被拒绝执行。
            // 5、 任务执行,5秒后,如果无任务可执行,销毁临时创建的5个线程
        }
        /*
            任务提交成功 :0
            任务提交成功 :1
            任务提交成功 :2
            任务提交成功 :3
            任务提交成功 :4
            任务提交成功 :5
            任务提交成功 :6
            任务提交成功 :7
            任务提交成功 :8
            任务提交成功 :9
            任务提交成功 :10
            任务提交成功 :11
            任务提交成功 :12
            任务提交成功 :13
            任务提交成功 :14
            有任务被拒绝执行了
            有任务被拒绝执行了
            开始执行:0
            开始执行:1
            开始执行:2
            开始执行:3
            开始执行:4
            开始执行:8
            开始执行:9
            开始执行:10
            开始执行:11
            开始执行:12
            当前线程池线程数量为:10
            当前线程池等待的数量为:3
            执行结束:0
            执行结束:1
            执行结束:2
            执行结束:3
            执行结束:4
            执行结束:8
            开始执行:5
            开始执行:6
            开始执行:7
            执行结束:9
            执行结束:10
            执行结束:11
            执行结束:12
            执行结束:5
            执行结束:6
            执行结束:7
         */
    

    通过Executors创建线程池

     线程池创建策略

    1、核心线程数5,最大线程数10,队列大小3,超出存活时间5秒,指定拒绝策略   (不指定拒绝策略会抛出异常)

    可以得出最大容纳量为13个任务

    5个任务直接分配线程开始执行

    3个任务进入等待队列队列不够用,临时加开5个线程来执行任务(5秒没活干就销毁)

    队列和线程池都满了,剩下2个任务,没资源了,被拒绝执行。

    任务执行,5秒后,如果无任务可执行,销毁临时创建的5个线程。

     

    2、 核心线程数量5,最大线程数量5,无界队列,超出存活时间5秒

    3、核心线程数量0,最大数量Integer.MAX_VALUE,SynchronousQueue队列,超出核心线程数量的线程存活时间:60秒   (针对不知道数量的线程数量)

    核心线程数量为0,SynchronousQueue维护一组线程会根据队列中没有空闲的线程而为新任务加开一个新的线程,空闲线程的存活时间为60秒,超过60秒,就会被回收

     

    4、定时线程任务执行:核心线程数量5,最大数量Integer.MAX_VALUE,DelayedWorkQueue延时队列,超出核心线程数量的线程存活时间0秒,延迟3秒

    3秒后执行,一次性任务,到点就执行

     

    5、定时任务周期性执行

    核心线程数量5,最大数量Integer.MAX_VALUE,DelayedWorkQueue延时队列,超出核心线程数量的线程存活时间0秒,延迟2秒,周期1秒,(如果发现上次执行还未完毕,则等待完毕,完毕后立刻执行)

    核心线程数量5,最大数量Integer.MAX_VALUE,DelayedWorkQueue延时队列,超出核心线程数量的线程存活时间0秒,延迟2秒,周期1秒,(如果发现上次执行还未完毕,则等待完毕,完毕后等待周期时间再执行)。  

     以上部分代码和图片摘自网易云课堂

  • 相关阅读:
    【255】◀▶IEW-Unit20
    【254】◀▶IEW-Unit19
    【253】◀▶IEW-Unit18
    【252】◀▶IEW-Unit17
    [LeetCode] Restore IP Address
    1030
    (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
    新知识的遗忘数度真是可怕
    【自由谈】城域网IPv6过渡技术——4v6场景技术总结(1)
    Graph Databases—The NOSQL Phenomenon阅读笔记
  • 原文地址:https://www.cnblogs.com/shuzhixia/p/13305772.html
Copyright © 2011-2022 走看看