1. 线程池 1.1 线程状态介绍 当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态。线程对象在不同的时期有不同的状态。那么 Java 中的线程存在哪几种状态呢?Java 中的线程
状态被定义在了 java.lang.Thread.State 枚举类中,State 枚举类的源码如下:
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 public class Thread { public enum State { NEW , RUNNABLE , BLOCKED , WAITING , TIMED_WAITING , TERMINATED; } public State getState () { return jdk.internal.misc.VM.toThreadState(threadStatus); } }
通过源码我们可以看到 Java 中的线程存在 6 种状态,每种线程状态的含义如下
线程状态
具体含义
NEW
一个尚未启动的线程的状态。也称之为初始状态、开始状态。线程刚被创建,但是并未启动。还没调用 start 方法。MyThread t = new MyThread()只有线程象,没有线程特征。
RUNNABLE
当我们调用线程对象的 start 方法,那么此时线程对象进入了 RUNNABLE 状态。那么此时才是真正的在 JVM 进程中创建了一个线程,线程一经启动并不是立即得到执行,线程的运行与否要听令与 CPU 的调度,那么我们把这个中间状态称之为可执行状态(RUNNABLE)也就是说它具备执行的资格,但是并没有真正的执行起来而是在等待 CPU 的度。
BLOCKED
当一个线程试图获取一个对象锁,而该对象锁被其他的线程持有,则该线程进入 Blocked 状态;当该线程持有锁时,该线程将变成 Runnable 状态。
WAITING
一个正在等待的线程的状态。也称之为等待状态。造成线程等待的原因有两种,分别是调用 Object.wait()、join()方法。处于等待状态的线程,正在等待其他线程去执行一个特定的操作。例如:因为 wait()而等待的线程正在等待另一个线程去调用 notify()或 notifyAll();一个因为 join()而等待的线程正在等待另一个线程结束。
TIMED_WAITING
一个在限定时间内等待的线程的状态。也称之为限时等待状态。造成线程限时等待状态的原因有三种,分别是:Thread.sleep(long),Object.wait(long)、join(long)。
TERMINATED
一个完全运行完成的线程的状态。也称之为终止状态、结束状态
各个状态的转换,如下图所示:
1.2 线程池-基本原理 概述 :
提到池,大家应该能想到的就是水池。水池就是一个容器,在该容器中存储了很多的水。那么什么是线程池呢?线程池也是可以看做成一个池子,在该池子中存储很多个线程。
线程池存在的意义:
系统创建一个线程的成本是比较高的,因为它涉及到与操作系统交互,当程序中需要创建大量生存期很短暂的线程时,频繁的创建和销毁线程对系统的资源消耗有可能大于业务处理是对系
统资源的消耗,这样就有点”舍本逐末”了。针对这一种情况,为了提高性能,我们就可以采用线程池。线程池在启动的时,会创建大量空闲线程,当我们向线程池提交任务的时,线程池就
会启动一个线程来执行该任务。等待任务执行完毕以后,线程并不会死亡,而是再次返回到线程池中称为空闲状态。等待下一次任务的执行。
线程池的设计思路 :
准备一个任务容器
一次性启动多个(2 个)消费者线程
刚开始任务容器是空的,所以线程都在 wait
直到一个外部线程向这个任务容器中扔了一个”任务”,就会有一个消费者线程被唤醒
这个消费者线程取出”任务”,并且执行这个任务,执行完毕后,继续等待下一次任务的到来
1.3 线程池-Executors 默认线程池 概述 : JDK 对线程池也进行了相关的实现,在真实企业开发中我们也很少去自定义线程池,而是使用 JDK 中自带的线程池。
我们可以使用 Executors 中所提供的静态 方法来创建线程池
static ExecutorService newCachedThreadPool() 创建一个默认的线程池 static newFixedThreadPool(int nThreads) 创建一个指定最多线程数量的线程池
代码实现 :
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 package com.itheima.mythreadpool;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MyThreadPoolDemo { public static void main (String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(()->{ System.out.println(Thread.currentThread().getName() + "在执行了" ); }); executorService.submit(()->{ System.out.println(Thread.currentThread().getName() + "在执行了" ); }); executorService.shutdown(); } }
1.4 线程池-Executors 创建指定上限的线程池 使用 Executors 中所提供的静态方法来创建线程池
static ExecutorService newFixedThreadPool(int nThreads) : 创建一个指定最多线程数量的线程池
代码实现 :
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 package com.itheima.mythreadpool;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadPoolExecutor;public class MyThreadPoolDemo2 { public static void main (String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(10 ); ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService; System.out.println(pool.getPoolSize()); executorService.submit(()->{ System.out.println(Thread.currentThread().getName() + "在执行了" ); }); executorService.submit(()->{ System.out.println(Thread.currentThread().getName() + "在执行了" ); }); System.out.println(pool.getPoolSize()); } }
1.5 线程池-ThreadPoolExecutor 创建线程池对象 :
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(核心线程数量,最大线程数量,空闲线程最大存活时间,任务队列,创建线程工厂,任务的拒绝策略);
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.itheima.mythreadpool;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.Executors;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class MyThreadPoolDemo3 { public static void main (String[] args) { ThreadPoolExecutor pool = new ThreadPoolExecutor (2 ,5 ,2 ,TimeUnit.SECONDS,new ArrayBlockingQueue <>(10 ), Executors.defaultThreadFactory(),new ThreadPoolExecutor .AbortPolicy()); pool.submit(new MyRunnable ()); pool.submit(new MyRunnable ()); pool.shutdown(); } }
1.6 线程池-参数详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) corePoolSize: 核心线程的最大值,不能小于0 maximumPoolSize:最大线程数,不能小于等于0 ,maximumPoolSize >= corePoolSize keepAliveTime: 空闲线程最大存活时间,不能小于0 unit: 时间单位 workQueue: 任务队列,不能为null threadFactory: 创建线程工厂,不能为null handler: 任务的拒绝策略,不能为null
1.7 线程池-非默认任务拒绝策略 RejectedExecutionHandler 是 jdk 提供的一个任务拒绝策略接口,它下面存在 4 个子类。
1 2 3 4 ThreadPoolExecutor.AbortPolicy: 丢弃任务并抛出RejectedExecutionException异常。是默认的策略。 ThreadPoolExecutor.DiscardPolicy: 丢弃任务,但是不抛出异常 这是不推荐的做法。 ThreadPoolExecutor.DiscardOldestPolicy: 抛弃队列中等待最久的任务 然后把当前任务加入队列中。 ThreadPoolExecutor.CallerRunsPolicy: 调用任务的run()方法绕过线程池直接执行。
注:明确线程池对多可执行的任务数 = 队列容量 + 最大线程数
案例演示 1 :演示 ThreadPoolExecutor.AbortPolicy 任务处理策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class ThreadPoolExecutorDemo01 { public static void main (String[] args) { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (1 , 3 , 20 , TimeUnit.SECONDS , new ArrayBlockingQueue <>(1 ) , Executors.defaultThreadFactory() , new ThreadPoolExecutor .AbortPolicy()) ; for (int x = 0 ; x < 5 ; x++) { threadPoolExecutor.submit(() -> { System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" ); }); } } }
控制台输出结果
1 2 3 4 pool-1 -thread-1 ---->> 执行了任务 pool-1 -thread-3 ---->> 执行了任务 pool-1 -thread-2 ---->> 执行了任务 pool-1 -thread-3 ---->> 执行了任务
控制台报错,仅仅执行了 4 个任务,有一个任务被丢弃了
案例演示 2 :演示 ThreadPoolExecutor.DiscardPolicy 任务处理策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class ThreadPoolExecutorDemo02 { public static void main (String[] args) { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (1 , 3 , 20 , TimeUnit.SECONDS , new ArrayBlockingQueue <>(1 ) , Executors.defaultThreadFactory() , new ThreadPoolExecutor .DiscardPolicy()) ; for (int x = 0 ; x < 5 ; x++) { threadPoolExecutor.submit(() -> { System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" ); }); } } }
控制台输出结果
1 2 3 4 pool-1 -thread-1 ---->> 执行了任务 pool-1 -thread-1 ---->> 执行了任务 pool-1 -thread-3 ---->> 执行了任务 pool-1 -thread-2 ---->> 执行了任务
控制台没有报错,仅仅执行了 4 个任务,有一个任务被丢弃了
案例演示 3 :演示 ThreadPoolExecutor.DiscardOldestPolicy 任务处理策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class ThreadPoolExecutorDemo02 { public static void main (String[] args) { ThreadPoolExecutor threadPoolExecutor; threadPoolExecutor = new ThreadPoolExecutor (1 , 3 , 20 , TimeUnit.SECONDS , new ArrayBlockingQueue <>(1 ) , Executors.defaultThreadFactory() , new ThreadPoolExecutor .DiscardOldestPolicy()); for (int x = 0 ; x < 5 ; x++) { final int y = x ; threadPoolExecutor.submit(() -> { System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" + y); }); } } }
控制台输出结果
1 2 3 4 pool-1 -thread-2 ---->> 执行了任务2 pool-1 -thread-1 ---->> 执行了任务0 pool-1 -thread-3 ---->> 执行了任务3 pool-1 -thread-1 ---->> 执行了任务4
由于任务 1 在线程池中等待时间最长,因此任务 1 被丢弃。
案例演示 4 :演示 ThreadPoolExecutor.CallerRunsPolicy 任务处理策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class ThreadPoolExecutorDemo04 { public static void main (String[] args) { ThreadPoolExecutor threadPoolExecutor; threadPoolExecutor = new ThreadPoolExecutor (1 , 3 , 20 , TimeUnit.SECONDS , new ArrayBlockingQueue <>(1 ) , Executors.defaultThreadFactory() , new ThreadPoolExecutor .CallerRunsPolicy()); for (int x = 0 ; x < 5 ; x++) { threadPoolExecutor.submit(() -> { System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" ); }); } } }
控制台输出结果
1 2 3 4 5 pool-1 -thread-1 ---->> 执行了任务 pool-1 -thread-3 ---->> 执行了任务 pool-1 -thread-2 ---->> 执行了任务 pool-1 -thread-1 ---->> 执行了任务 main---->> 执行了任务
通过控制台的输出,我们可以看到次策略没有通过线程池中的线程执行任务,而是直接调用任务的 run()方法绕过线程池直接执行。
2. 多线程综合练习 练习一:售票 需求:
一共有 1000 张电影票,可以在两个窗口领取,假设每次领取的时间为 3000 毫秒,
请用多线程模拟卖票过程并打印剩余电影票的数量
代码示例:
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 public class MyThread extends Thread { static int ticket = 1000 ; @Override public void run () { while (true ) { synchronized (MyThread.class) { if (ticket == 0 ) { break ; } else { try { Thread.sleep(3000 ); } catch (InterruptedException e) { e.printStackTrace(); } ticket--; System.out.println(getName() + "在卖票,还剩下" + ticket + "张票!!!" ); } } } } } public class Test { public static void main (String[] args) { MyThread t1 = new MyThread (); MyThread t2 = new MyThread (); t1.setName("窗口1" ); t2.setName("窗口2" ); t1.start(); t2.start(); } }
练习二:赠送礼物 需求:
有 100 份礼品,两人同时发送,当剩下的礼品小于 10 份的时候则不再送出。
利用多线程模拟该过程并将线程的名字和礼物的剩余数量打印出来.
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 public class MyRunable implements Runnable { int count = 100 ; @Override public void run () { while (true ) { synchronized (MyThread.class) { if (count < 10 ) { System.out.println("礼物还剩下" + count + "不再赠送" ); break ; } else { count--; System.out.println(Thread.currentThread().getName() + "在赠送礼物,还剩下" + count + "个礼物!!!" ); } } } } } public class Test { public static void main (String[] args) { MyRunable mr = new MyRunable (); Thread t1 = new Thread (mr,"窗口1" ); Thread t2 = new Thread (mr,"窗口2" ); t1.start(); t2.start(); } }
练习三:打印数字 需求:
同时开启两个线程,共同获取 1-100 之间的所有数字。
将输出所有的奇数。
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 public class MyRunable implements Runnable { int number = 1 ; @Override public void run () { while (true ) { synchronized (MyThread.class) { if (number > 100 ) { break ; } else { if (number % 2 == 1 ){ System.out.println(Thread.currentThread().getName() + "打印数字" + number); } number++; } } } } } public class Test { public static void main (String[] args) { MyRunable mr = new MyRunable (); Thread t1 = new Thread (mr,"线程A" ); Thread t2 = new Thread (mr,"线程B" ); t1.start(); t2.start(); } }
练习四:抢红包 需求:
抢红包也用到了多线程。
假设:100 块,分成了 3 个包,现在有 5 个人去抢。
其中,红包是共享数据。
5 个人是 5 条线程。
打印结果如下:
XXX 抢到了 XXX 元
XXX 抢到了 XXX 元
XXX 抢到了 XXX 元
XXX 没抢到
XXX 没抢到
解决方案一:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 public class MyThread extends Thread { static double money = 100 ; static int count = 3 ; static final double MIN = 0.01 ; @Override public void run () { synchronized (MyThread.class){ if (count == 0 ){ System.out.println(getName() + "没有抢到红包!" ); }else { double prize = 0 ; if (count == 1 ){ prize = money; }else { Random r = new Random (); double bounds = money - (count - 1 ) * MIN; prize = r.nextDouble(bounds); if (prize < MIN){ prize = MIN; } } money = money - prize; count--; System.out.println(getName() + "抢到了" + prize + "元" ); } } } } public class Test { public static void main (String[] args) { MyThread t1 = new MyThread (); MyThread t2 = new MyThread (); MyThread t3 = new MyThread (); MyThread t4 = new MyThread (); MyThread t5 = new MyThread (); t1.setName("小A" ); t2.setName("小QQ" ); t3.setName("小哈哈" ); t4.setName("小诗诗" ); t5.setName("小丹丹" ); t1.start(); t2.start(); t3.start(); t4.start(); t5.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 public class MyThread extends Thread { static BigDecimal money = BigDecimal.valueOf(100.0 ); static int count = 3 ; static final BigDecimal MIN = BigDecimal.valueOf(0.01 ); @Override public void run () { synchronized (MyThread.class){ if (count == 0 ){ System.out.println(getName() + "没有抢到红包!" ); }else { BigDecimal prize; if (count == 1 ){ prize = money; }else { double bounds = money.subtract(BigDecimal.valueOf(count-1 ).multiply(MIN)).doubleValue(); Random r = new Random (); prize = BigDecimal.valueOf(r.nextDouble(bounds)); } prize = prize.setScale(2 ,RoundingMode.HALF_UP); money = money.subtract(prize); count--; System.out.println(getName() + "抽中了" + prize + "元" ); } } } } public class Test { public static void main (String[] args) { MyThread t1 = new MyThread (); MyThread t2 = new MyThread (); MyThread t3 = new MyThread (); MyThread t4 = new MyThread (); MyThread t5 = new MyThread (); t1.setName("小A" ); t2.setName("小QQ" ); t3.setName("小哈哈" ); t4.setName("小诗诗" ); t5.setName("小丹丹" ); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); } }
练习五:抽奖箱 需求:
有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池中的奖项为 {10,5,20,50,100,200,500,800,2,80,300,700};
创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱 1”,“抽奖箱 2”
随机从抽奖池中获取奖项元素并打印在控制台上,格式如下:
每次抽出一个奖项就打印一个(随机)
抽奖箱 1 又产生了一个 10 元大奖
抽奖箱 1 又产生了一个 100 元大奖
抽奖箱 1 又产生了一个 200 元大奖
抽奖箱 1 又产生了一个 800 元大奖
抽奖箱 2 又产生了一个 700 元大奖
…..
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 public class MyThread extends Thread { ArrayList<Integer> list; public MyThread (ArrayList<Integer> list) { this .list = list; } @Override public void run () { while (true ) { synchronized (MyThread.class) { if (list.size() == 0 ) { break ; } else { Collections.shuffle(list); int prize = list.remove(0 ); System.out.println(getName() + "又产生了一个" + prize + "元大奖" ); } } try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList <>(); Collections.addAll(list,10 ,5 ,20 ,50 ,100 ,200 ,500 ,800 ,2 ,80 ,300 ,700 ); MyThread t1 = new MyThread (list); MyThread t2 = new MyThread (list); t1.setName("抽奖箱1" ); t2.setName("抽奖箱2" ); t1.start(); t2.start(); } }
练习六:多线程统计并求最大值 需求:
在上一题基础上继续完成如下需求:
每次抽的过程中,不打印,抽完时一次性打印(随机)
在此次抽奖过程中,抽奖箱 1 总共产生了 6 个奖项。
分别为:10,20,100,500,2,300 最高奖项为 300 元,总计额为 932 元
在此次抽奖过程中,抽奖箱 2 总共产生了 6 个奖项。
分别为:5,50,200,800,80,700 最高奖项为 800 元,总计额为 1835 元
解决方案一:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 public class MyThread extends Thread { ArrayList<Integer> list; public MyThread (ArrayList<Integer> list) { this .list = list; } static ArrayList<Integer> list1 = new ArrayList <>(); static ArrayList<Integer> list2 = new ArrayList <>(); @Override public void run () { while (true ) { synchronized (MyThread.class) { if (list.size() == 0 ) { if ("抽奖箱1" .equals(getName())){ System.out.println("抽奖箱1" + list1); }else { System.out.println("抽奖箱2" + list2); } break ; } else { Collections.shuffle(list); int prize = list.remove(0 ); if ("抽奖箱1" .equals(getName())){ list1.add(prize); }else { list2.add(prize); } } } try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList <>(); Collections.addAll(list,10 ,5 ,20 ,50 ,100 ,200 ,500 ,800 ,2 ,80 ,300 ,700 ); MyThread t1 = new MyThread (list); MyThread t2 = new MyThread (list); t1.setName("抽奖箱1" ); t2.setName("抽奖箱2" ); t1.start(); t2.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 public class MyThread extends Thread { ArrayList<Integer> list; public MyThread (ArrayList<Integer> list) { this .list = list; } @Override public void run () { ArrayList<Integer> boxList = new ArrayList <>(); while (true ) { synchronized (MyThread.class) { if (list.size() == 0 ) { System.out.println(getName() + boxList); break ; } else { Collections.shuffle(list); int prize = list.remove(0 ); boxList.add(prize); } } try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Test { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList <>(); Collections.addAll(list,10 ,5 ,20 ,50 ,100 ,200 ,500 ,800 ,2 ,80 ,300 ,700 ); MyThread t1 = new MyThread (list); MyThread t2 = new MyThread (list); t1.setName("抽奖箱1" ); t2.setName("抽奖箱2" ); t1.start(); t2.start(); } }
练习七:多线程之间的比较 需求:
在上一题基础上继续完成如下需求:
在此次抽奖过程中,抽奖箱 1 总共产生了 6 个奖项,分别为:10,20,100,500,2,300
最高奖项为 300 元,总计额为 932 元
在此次抽奖过程中,抽奖箱 2 总共产生了 6 个奖项,分别为:5,50,200,800,80,700
最高奖项为 800 元,总计额为 1835 元
在此次抽奖过程中,抽奖箱 2 中产生了最大奖项,该奖项金额为 800 元
以上打印效果只是数据模拟,实际代码运行的效果会有差异
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 public class MyCallable implements Callable <Integer> { ArrayList<Integer> list; public MyCallable (ArrayList<Integer> list) { this .list = list; } @Override public Integer call () throws Exception { ArrayList<Integer> boxList = new ArrayList <>(); while (true ) { synchronized (MyCallable.class) { if (list.size() == 0 ) { System.out.println(Thread.currentThread().getName() + boxList); break ; } else { Collections.shuffle(list); int prize = list.remove(0 ); boxList.add(prize); } } Thread.sleep(10 ); } if (boxList.size() == 0 ){ return null ; }else { return Collections.max(boxList); } } } package com.itheima.test7;import java.util.ArrayList;import java.util.Collections;import java.util.concurrent.ExecutionException;import java.util.concurrent.FutureTask;public class Test { public static void main (String[] args) throws ExecutionException, InterruptedException { ArrayList<Integer> list = new ArrayList <>(); Collections.addAll(list,10 ,5 ,20 ,50 ,100 ,200 ,500 ,800 ,2 ,80 ,300 ,700 ); MyCallable mc = new MyCallable (list); FutureTask<Integer> ft1 = new FutureTask <>(mc); FutureTask<Integer> ft2 = new FutureTask <>(mc); Thread t1 = new Thread (ft1); Thread t2 = new Thread (ft2); t1.setName("抽奖箱1" ); t2.setName("抽奖箱2" ); t1.start(); t2.start(); Integer max1 = ft1.get(); Integer max2 = ft2.get(); System.out.println(max1); System.out.println(max2); if (max1 == null ){ System.out.println("在此次抽奖过程中,抽奖箱2中产生了最大奖项,该奖项金额为" +max2+"元" ); }else if (max2 == null ){ System.out.println("在此次抽奖过程中,抽奖箱1中产生了最大奖项,该奖项金额为" +max1+"元" ); }else if (max1 > max2){ System.out.println("在此次抽奖过程中,抽奖箱1中产生了最大奖项,该奖项金额为" +max1+"元" ); }else if (max1 < max2){ System.out.println("在此次抽奖过程中,抽奖箱2中产生了最大奖项,该奖项金额为" +max2+"元" ); }else { System.out.println("两者的最大奖项是一样的" ); } } }
2. 原子性 2.1 volatile-问题 代码分析 :
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.myvolatile;public class Demo { public static void main (String[] args) { MyThread1 t1 = new MyThread1 (); t1.setName("小路同学" ); t1.start(); MyThread2 t2 = new MyThread2 (); t2.setName("小皮同学" ); t2.start(); } }
1 2 3 4 5 package com.itheima.myvolatile;public class Money { public static int money = 100000 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.myvolatile;public class MyThread1 extends Thread { @Override public void run () { while (Money.money == 100000 ){ } System.out.println("结婚基金已经不是十万了" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.itheima.myvolatile;public class MyThread2 extends Thread { @Override public void run () { try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } Money.money = 90000 ; } }
程序问题 : 女孩虽然知道结婚基金是十万,但是当基金的余额发生变化的时候,女孩无法知道最新的余额。
2.2 volatile 解决 以上案例出现的问题 :
当 A 线程修改了共享数据时,B 线程没有及时获取到最新的值,如果还在使用原先的值,就会出现问题
1,堆内存是唯一的,每一个线程都有自己的线程栈。
2 ,每一个线程在使用堆里面变量的时候,都会先拷贝一份到变量的副本中。
3 ,在线程中,每一次使用是从变量的副本中获取的。
Volatile 关键字 : 强制线程每次在使用的时候,都会看一下共享区域最新的值
代码实现 : 使用 volatile 关键字解决
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.myvolatile;public class Demo { public static void main (String[] args) { MyThread1 t1 = new MyThread1 (); t1.setName("小路同学" ); t1.start(); MyThread2 t2 = new MyThread2 (); t2.setName("小皮同学" ); t2.start(); } }
1 2 3 4 5 package com.itheima.myvolatile;public class Money { public static volatile int money = 100000 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.myvolatile;public class MyThread1 extends Thread { @Override public void run () { while (Money.money == 100000 ){ } System.out.println("结婚基金已经不是十万了" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.itheima.myvolatile;public class MyThread2 extends Thread { @Override public void run () { try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } Money.money = 90000 ; } }
2.3 synchronized 解决 synchronized 解决 :
1 ,线程获得锁
2 ,清空变量副本
3 ,拷贝共享变量最新的值到变量副本中
4 ,执行代码
5 ,将修改后变量副本中的值赋值给共享数据
6 ,释放锁
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.itheima.myvolatile2;public class Demo { public static void main (String[] args) { MyThread1 t1 = new MyThread1 (); t1.setName("小路同学" ); t1.start(); MyThread2 t2 = new MyThread2 (); t2.setName("小皮同学" ); t2.start(); } }
1 2 3 4 5 6 package com.itheima.myvolatile2;public class Money { public static Object lock = new Object (); public static volatile int money = 100000 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.itheima.myvolatile2;public class MyThread1 extends Thread { @Override public void run () { while (true ){ synchronized (Money.lock){ if (Money.money != 100000 ){ System.out.println("结婚基金已经不是十万了" ); break ; } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.itheima.myvolatile2;public class MyThread2 extends Thread { @Override public void run () { synchronized (Money.lock) { try { Thread.sleep(10 ); } catch (InterruptedException e) { e.printStackTrace(); } Money.money = 90000 ; } } }
2.4 原子性 概述 : 所谓的原子性是指在一次操作或者多次操作中,要么所有的操作全部都得到了执行并且不会受到任何因素的干扰而中断,要么所有的操作都不执行,多个操作是一个不可以分割的整体。
代码实现 :
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 package com.itheima.threadatom;public class AtomDemo { public static void main (String[] args) { MyAtomThread atom = new MyAtomThread (); for (int i = 0 ; i < 100 ; i++) { new Thread (atom).start(); } } } class MyAtomThread implements Runnable { private volatile int count = 0 ; @Override public void run () { for (int i = 0 ; i < 100 ; i++) { count++; System.out.println("已经送了" + count + "个冰淇淋" ); } } }
代码总结 : count++ 不是一个原子性操作, 他在执行的过程中,有可能被其他线程打断
2.5 volatile 关键字不能保证原子性 解决方案 : 我们可以给 count++操作添加锁,那么 count++操作就是临界区中的代码,临界区中的代码一次只能被一个线程去执行,所以 count++就变成了原子操作。
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 package com.itheima.threadatom2;public class AtomDemo { public static void main (String[] args) { MyAtomThread atom = new MyAtomThread (); for (int i = 0 ; i < 100 ; i++) { new Thread (atom).start(); } } } class MyAtomThread implements Runnable { private volatile int count = 0 ; private Object lock = new Object (); @Override public void run () { for (int i = 0 ; i < 100 ; i++) { synchronized (lock) { count++; System.out.println("已经送了" + count + "个冰淇淋" ); } } } }
2.6 原子性_AtomicInteger 概述:java 从 JDK1.5 开始提供了 java.util.concurrent.atomic 包(简称 Atomic 包),这个包中的原子操作类提供了一种用法简单,性能高效,线程安全地更新一个变量的方式。因为变
量的类型有很多种,所以在 Atomic 包里一共提供了 13 个类,属于 4 种类型的原子更新方式,分别是原子更新基本类型、原子更新数组、原子更新引用和原子更新属性(字段)。本次我们只讲解
使用原子的方式更新基本类型,使用原子的方式更新基本类型 Atomic 包提供了以下 3 个类:
AtomicBoolean: 原子更新布尔类型
AtomicInteger: 原子更新整型
AtomicLong: 原子更新长整型
以上 3 个类提供的方法几乎一模一样,所以本节仅以 AtomicInteger 为例进行讲解,AtomicInteger 的常用方法如下:
1 2 3 4 5 6 7 8 public AtomicInteger () : 初始化一个默认值为0 的原子型Integerpublic AtomicInteger (int initialValue) : 初始化一个指定值的原子型Integerint get () : 获取值int getAndIncrement () : 以原子方式将当前值加1 ,注意,这里返回的是自增前的值。int incrementAndGet () : 以原子方式将当前值加1 ,注意,这里返回的是自增后的值。int addAndGet (int data) : 以原子方式将输入的数值与实例中的值(AtomicInteger里的value)相加,并返回结果。int getAndSet (int value) : 以原子方式设置为newValue的值,并返回旧值。
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.itheima.threadatom3;import java.util.concurrent.atomic.AtomicInteger;public class MyAtomIntergerDemo1 { public static void main (String[] args) { AtomicInteger ac = new AtomicInteger (); System.out.println(ac); AtomicInteger ac2 = new AtomicInteger (10 ); System.out.println(ac2); } }
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 package com.itheima.threadatom3;import java.lang.reflect.Field;import java.util.concurrent.atomic.AtomicInteger;public class MyAtomIntergerDemo2 { public static void main (String[] args) { AtomicInteger ac5 = new AtomicInteger (100 ); int andSet = ac5.getAndSet(20 ); System.out.println(andSet); System.out.println(ac5.get()); } }
2.7 AtomicInteger-内存解析 AtomicInteger 原理 : 自旋锁 + CAS 算法
CAS 算法:
有 3 个操作数(内存值 V, 旧的预期值 A,要修改的值 B)
当旧的预期值 A == 内存值 此时修改成功,将 V 改为 B
当旧的预期值 A!=内存值 此时修改失败,不做任何操作
并重新获取现在的最新值(这个重新获取的动作就是自旋)
2.8 AtomicInteger-源码解析 代码实现 :
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.threadatom4;public class AtomDemo { public static void main (String[] args) { MyAtomThread atom = new MyAtomThread (); for (int i = 0 ; i < 100 ; i++) { new Thread (atom).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 package com.itheima.threadatom4;import java.util.concurrent.atomic.AtomicInteger;public class MyAtomThread implements Runnable { AtomicInteger ac = new AtomicInteger (0 ); @Override public void run () { for (int i = 0 ; i < 100 ; i++) { int count = ac.incrementAndGet(); System.out.println("已经送了" + count + "个冰淇淋" ); } } }
源码解析 :
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 public final int incrementAndGet () { return U.getAndAddInt(this , VALUE, 1 ) + 1 ; } public final int getAndAddInt (Object o, long offset, int delta) { int v; do { v = getIntVolatile(o, offset); } while (!weakCompareAndSetInt(o, offset, v, v + delta)); return v; }
2.9 悲观锁和乐观锁 synchronized 和 CAS 的区别 :
相同点: 在多线程情况下,都可以保证共享数据的安全性。
不同点: synchronized 总是从最坏的角度出发,认为每次获取数据的时候,别人都有可能修改。所以在每 次操作共享数据之前,都会上锁。(悲观锁)
cas 是从乐观的角度出发,假设每次获取数据别人都不会修改,所以不会上锁。只不过在修改共享数据的时候,会检查一下,别人有没有修改过这个数据。
如果别人修改过,那么我再次获取现在最新的值。
如果别人没有修改过,那么我现在直接修改共享数据的值.(乐观锁)
3. 并发工具类 3.1 并发工具类-Hashtable Hashtable 出现的原因 : 在集合类中 HashMap 是比较常用的集合对象,但是 HashMap 是线程不安全的(多线程环境下可能会存在问题)。为了保证数据的安全性我们可以使用 Hashtable,但是 Hashtable 的效率低下。
代码实现 :
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 package com.itheima.mymap;import java.util.HashMap;import java.util.Hashtable;public class MyHashtableDemo { public static void main (String[] args) throws InterruptedException { Hashtable<String, String> hm = new Hashtable <>(); Thread t1 = new Thread (() -> { for (int i = 0 ; i < 25 ; i++) { hm.put(i + "" , i + "" ); } }); Thread t2 = new Thread (() -> { for (int i = 25 ; i < 51 ; i++) { hm.put(i + "" , i + "" ); } }); t1.start(); t2.start(); System.out.println("----------------------------" ); Thread.sleep(1000 ); for (int i = 0 ; i < 51 ; i++) { System.out.println(hm.get(i + "" )); } } }
3.2 并发工具类-ConcurrentHashMap 基本使用 ConcurrentHashMap 出现的原因 : 在集合类中 HashMap 是比较常用的集合对象,但是 HashMap 是线程不安全的(多线程环境下可能会存在问题)。为了保证数据的安全性我们可以使用 Hashtable,但是 Hashtable 的效率低下。
基于以上两个原因我们可以使用 JDK1.5 以后所提供的 ConcurrentHashMap。
体系结构 :
总结 :
1 ,HashMap 是线程不安全的。多线程环境下会有数据安全问题
2 ,Hashtable 是线程安全的,但是会将整张表锁起来,效率低下
3,ConcurrentHashMap 也是线程安全的,效率较高。 在 JDK7 和 JDK8 中,底层原理不一样。
代码实现 :
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 package com.itheima.mymap;import java.util.Hashtable;import java.util.concurrent.ConcurrentHashMap;public class MyConcurrentHashMapDemo { public static void main (String[] args) throws InterruptedException { ConcurrentHashMap<String, String> hm = new ConcurrentHashMap <>(100 ); Thread t1 = new Thread (() -> { for (int i = 0 ; i < 25 ; i++) { hm.put(i + "" , i + "" ); } }); Thread t2 = new Thread (() -> { for (int i = 25 ; i < 51 ; i++) { hm.put(i + "" , i + "" ); } }); t1.start(); t2.start(); System.out.println("----------------------------" ); Thread.sleep(1000 ); for (int i = 0 ; i < 51 ; i++) { System.out.println(hm.get(i + "" )); } } }
3.3 并发工具类-ConcurrentHashMap1.7 原理
3.4 并发工具类-ConcurrentHashMap1.8 原理
总结 :
1,如果使用空参构造创建 ConcurrentHashMap 对象,则什么事情都不做。 在第一次添加元素的时候创建哈希表
2,计算当前元素应存入的索引。
3,如果该索引位置为 null,则利用 cas 算法,将本结点添加到数组中。
4,如果该索引位置不为 null,则利用 volatile 关键字获得当前位置最新的结点地址,挂在他下面,变成链表。
5,当链表的长度大于等于 8 时,自动转换成红黑树 6,以链表或者红黑树头结点为锁对象,配合悲观锁保证多线程操作集合时数据的安全性
3.5 并发工具类-CountDownLatch CountDownLatch 类 :
方法
解释
public CountDownLatch(int count)
参数传递线程数,表示等待线程数量
public void await()
让线程等待
public void countDown()
当前线程执行完毕
使用场景: 让某一条线程等待其他线程执行完毕之后再执行
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.itheima.mycountdownlatch;import java.util.concurrent.CountDownLatch;public class ChileThread1 extends Thread { private CountDownLatch countDownLatch; public ChileThread1 (CountDownLatch countDownLatch) { this .countDownLatch = countDownLatch; } @Override public void run () { for (int i = 1 ; i <= 10 ; i++) { System.out.println(getName() + "在吃第" + i + "个饺子" ); } countDownLatch.countDown(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.itheima.mycountdownlatch;import java.util.concurrent.CountDownLatch;public class ChileThread2 extends Thread { private CountDownLatch countDownLatch; public ChileThread2 (CountDownLatch countDownLatch) { this .countDownLatch = countDownLatch; } @Override public void run () { for (int i = 1 ; i <= 15 ; i++) { System.out.println(getName() + "在吃第" + i + "个饺子" ); } countDownLatch.countDown(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.itheima.mycountdownlatch;import java.util.concurrent.CountDownLatch;public class ChileThread3 extends Thread { private CountDownLatch countDownLatch; public ChileThread3 (CountDownLatch countDownLatch) { this .countDownLatch = countDownLatch; } @Override public void run () { for (int i = 1 ; i <= 20 ; i++) { System.out.println(getName() + "在吃第" + i + "个饺子" ); } countDownLatch.countDown(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.itheima.mycountdownlatch;import java.util.concurrent.CountDownLatch;public class MotherThread extends Thread { private CountDownLatch countDownLatch; public MotherThread (CountDownLatch countDownLatch) { this .countDownLatch = countDownLatch; } @Override public void run () { try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("妈妈在收拾碗筷" ); } }
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 package com.itheima.mycountdownlatch;import java.util.concurrent.CountDownLatch;public class MyCountDownLatchDemo { public static void main (String[] args) { CountDownLatch countDownLatch = new CountDownLatch (3 ); MotherThread motherThread = new MotherThread (countDownLatch); motherThread.start(); ChileThread1 t1 = new ChileThread1 (countDownLatch); t1.setName("小明" ); ChileThread2 t2 = new ChileThread2 (countDownLatch); t2.setName("小红" ); ChileThread3 t3 = new ChileThread3 (countDownLatch); t3.setName("小刚" ); t1.start(); t2.start(); t3.start(); } }
总结 :
1. CountDownLatch(int count):参数写等待线程的数量。并定义了一个计数器。
2. await():让线程等待,当计数器为 0 时,会唤醒等待的线程
3. countDown(): 线程执行完毕时调用,会将计数器-1。
3.6 并发工具类-Semaphore 使用场景 :
可以控制访问特定资源的线程数量。
实现步骤 :
1,需要有人管理这个通道
2,当有车进来了,发通行许可证
3,当车出去了,收回通行许可证
4,如果通行许可证发完了,那么其他车辆只能等着
代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.itheima.mysemaphore;import java.util.concurrent.Semaphore;public class MyRunnable implements Runnable { private Semaphore semaphore = new Semaphore (2 ); @Override public void run () { try { semaphore.acquire(); System.out.println("获得了通行证开始行驶" ); Thread.sleep(2000 ); System.out.println("归还通行证" ); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.mysemaphore;public class MySemaphoreDemo { public static void main (String[] args) { MyRunnable mr = new MyRunnable (); for (int i = 0 ; i < 100 ; i++) { new Thread (mr).start(); } } }