1 实现多线程
1.1 简单了解多线程
是指从软件或者硬件上实现多个线程并发执行的技术。 具有多线程能力的计算机因有硬件支持而能够在同一时间执行多个线程,提升性能。
1.2 并发和并行
并行:在同一时刻,有多个指令在多个CPU上同时 执行。 并发:在同一时刻,有多个指令在单个CPU上交替 执行。
1.3 进程和线程
进程:是正在运行的程序
独立性:进程是一个能独立运行的基本单位,同时也是系统分配资源和调度的独立单位 动态性:进程的实质是程序的一次执行过程,进程是动态产生,动态消亡的 并发性:任何进程都可以同其他进程一起并发执行 线程:是进程中的单个顺序控制流,是一条执行路径
单线程:一个进程如果只有一条执行路径,则称为单线程程序 多线程:一个进程如果有多条执行路径,则称为多线程程序
1.4 实现多线程方式一:继承Thread类
方法名 说明 void run() 在线程开启后,此方法将被调用执行 void start() 使此线程开始执行,Java虚拟机会调用run方法()
实现步骤
定义一个类MyThread继承Thread类 在MyThread类中重写run()方法 创建MyThread类的对象 启动线程
public class MyThread extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( "线程开启了" + i) ;
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyThread t1 = new MyThread ( ) ;
MyThread t2 = new MyThread ( ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
两个小问题
为什么要重写run()方法?
run()方法和start()方法的区别?
run():封装线程执行的代码,直接调用,相当于普通方法的调用 start():启动线程;然后由JVM调用此线程的run()方法
1.5 实现多线程方式二:实现Runnable接口
方法名 说明 Thread(Runnable target) 分配一个新的Thread对象 Thread(Runnable target, String name) 分配一个新的Thread对象
实现步骤
定义一个类MyRunnable实现Runnable接口 在MyRunnable类中重写run()方法 创建MyRunnable类的对象 创建Thread类的对象,把MyRunnable对象作为构造方法的参数 启动线程
public class MyRunnable implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "第二种方式实现多线程" + i) ;
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyRunnable mr = new MyRunnable ( ) ;
Thread t1 = new Thread ( mr) ;
t1. start ( ) ;
MyRunnable mr2 = new MyRunnable ( ) ;
Thread t2 = new Thread ( mr2) ;
t2. start ( ) ;
}
}
1.6 实现多线程方式三: 实现Callable和Future接口
方法名 说明 V call() 计算结果,如果无法计算结果,则抛出一个异常 FutureTask(Callable< V > callable) 创建一个 FutureTask,一旦运行就执行给定的 Callable V get() 如有必要,等待计算完成,然后获取其结果
实现步骤
定义一个类MyCallable实现Callable接口 在MyCallable类中重写call()方法 创建MyCallable类的对象 创建Future的实现类FutureTask对象,把MyCallable对象作为构造方法的参数 创建Thread类的对象,把FutureTask对象作为构造方法的参数 启动线程 再调用get方法,就可以获取线程结束之后的结果。
import java. util. concurrent. Callable ;
public class MyCallable implements Callable < String > {
@Override
public String call ( ) throws Exception {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( "跟女孩表白" + i) ;
}
return "答应" ;
}
}
import java. util. concurrent. ExecutionException ;
import java. util. concurrent. FutureTask ;
public class Demo {
public static void main ( String [ ] args) throws ExecutionException , InterruptedException {
MyCallable mc = new MyCallable ( ) ;
FutureTask < String > ft = new FutureTask < > ( mc) ;
Thread t1 = new Thread ( ft) ;
t1. start ( ) ;
String s = ft. get ( ) ;
System . out. println ( s) ;
}
}
1.7 实现多线程三种方式的对比
实现Runnable、Callable接口
好处: 扩展性强,实现该接口的同时还可以继承其他的类 缺点: 编程相对复杂,不能直接使用Thread类中的方法 继承Thread类
好处: 编程比较简单,可以直接使用Thread类中的方法 缺点: 可以扩展性较差,不能再继承其他的类
1.8 线程类常见方法
1.8.1 获取和设置线程名称
方法名 说明 String getName() 返回此线程的名称 void setName(String name) 将此线程的名称更改为等于参数name 通过构造方法也可以设置线程名称
public class MyThread extends Thread {
public MyThread ( ) {
}
public MyThread ( String name) {
super ( name) ;
}
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( getName ( ) + "@@@" + i) ;
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyThread t1 = new MyThread ( "小蔡" ) ;
MyThread t2 = new MyThread ( "小强" ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
1.8.2 获得当前线程的对象
方法名 说明 Thread currentThread() 返回对当前正在执行的线程对象的引用
public class Demo {
public static void main ( String [ ] args) {
String name = Thread . currentThread ( ) . getName ( ) ;
System . out. println ( name) ;
}
}
1.8.3 线程休眠
方法名 说明 static void sleep(long millis) 使当前正在执行的线程停留(暂停执行)指定的毫秒数
public class MyRunnable implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "---" + i) ;
}
}
}
public class Demo {
public static void main ( String [ ] args) throws InterruptedException {
MyRunnable mr = new MyRunnable ( ) ;
Thread t1 = new Thread ( mr) ;
Thread t2 = new Thread ( mr) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
1.8.4 线程调度
多线程的并发运行
计算机中的CPU,在任意时刻只能执行一条机器指令。每个线程只有获得CPU的使用权才能执行代码。 各个线程轮流获得CPU的使用权,分别执行各自的任务。 线程有两种调度模型
分时调度模型:所有线程轮流 使用 CPU 的使用权,平均分配每个线程占用 CPU 的时间片 抢占式调度模型:优先让优先级高的线程使用 CPU,如果线程的优先级相同,那么会随机 选择一个,优先级高的线程 获取的 CPU 时间片相对多一些 Java使用的是抢占式调度模型 随机性
假如计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到CPU时间片,也就是使用权,才可以执行指令。所以说多线程程序的执行是有随机性,因为谁抢到CPU的使用权是不一定的
1.8.5 线程的优先级
方法名 说明 final void setPriority(int newPriority) 更改此线程的优先级线程默认优先级是5;线程优先级的范围是:1-10 final int getPriority() 返回此线程的优先级
import java. util. concurrent. Callable ;
public class MyCallable implements Callable < String > {
@Override
public String call ( ) throws Exception {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "---" + i) ;
}
return "线程执行完毕了" ;
}
}
import java. util. concurrent. FutureTask ;
public class Demo {
public static void main ( String [ ] args) {
MyCallable mc = new MyCallable ( ) ;
FutureTask < String > ft = new FutureTask < > ( mc) ;
Thread t1 = new Thread ( ft) ;
t1. setName ( "飞机" ) ;
t1. setPriority ( 10 ) ;
t1. start ( ) ;
MyCallable mc2 = new MyCallable ( ) ;
FutureTask < String > ft2 = new FutureTask < > ( mc2) ;
Thread t2 = new Thread ( ft2) ;
t2. setName ( "坦克" ) ;
t2. setPriority ( 1 ) ;
t2. start ( ) ;
}
}
1.8.6 后台线程/守护线程
方法名 说明 void setDaemon(boolean on) 将此线程标记为守护线程,当运行的线程都是守护线程时,Java虚拟机将退出
public class MyThread1 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( getName ( ) + "---" + i) ;
}
}
}
public class MyThread2 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 100 ; i++ ) {
System . out. println ( getName ( ) + "---" + i) ;
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyThread1 t1 = new MyThread1 ( ) ;
MyThread2 t2 = new MyThread2 ( ) ;
t1. setName ( "女神" ) ;
t2. setName ( "备胎" ) ;
t2. setDaemon ( true ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
1.8.7 线程生命周期
2 线程安全问题
2.1 案例1 卖票
案例需求
某电影院目前正在上映国产大片,共有100张票,而它有3个窗口卖票,请设计一个程序模拟该电影院卖票 思路
定义一个类Ticket实现Runnable接口,里面定义一个成员变量:private int ticketCount = 100; 在Ticket类中重写run()方法实现卖票,代码步骤如下
A:判断票数大于0,就卖票,并告知是哪个窗口卖的 B:票数要减1 C:卖光之后,线程停止 定义一个测试类TicketDemo,里面有main方法,代码步骤如下
A:创建Ticket类的对象 B:创建三个Thread类的对象,把Ticket对象作为构造方法的参数,并给出对应的窗口名称 C:启动线程 实现步骤
定义 在SellTicket类中重写run()方法实现卖票,代码步骤如下 判断票数大于0,就卖票,并告知是哪个窗口卖的 卖了票之后,总票数要减1 票卖没了,线程停止 定义一个测试类SellTicketDemo,里面有main方法,代码步骤如下 创建SellTicket类的对象 创建三个Thread类的对象,把SellTicket对象作为构造方法的参数,并给出对应的窗口名称 启动线程
public class Ticket implements Runnable {
private int ticket = 100 ;
@Override
public void run ( ) {
while ( true ) {
if ( ticket == 0 ) {
break ;
} else {
ticket-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticket + "张票" ) ;
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
Ticket ticket = new Ticket ( ) ;
Thread t1 = new Thread ( ticket) ;
Thread t2 = new Thread ( ticket) ;
Thread t3 = new Thread ( ticket) ;
t1. setName ( "窗口一" ) ;
t2. setName ( "窗口二" ) ;
t3. setName ( "窗口三" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
2.2 卖票案例的思考
刚才写的电影院卖票程序,好像没有什么问题。但是在实际生活中,售票时出票也是需要时间的, 所以,在出售一张票的时候,需要一点时间的延迟,接下来我们去修改卖票程序中卖票的动作: 每次出票时间100毫秒,用sleep()方法实现
public class Ticket implements Runnable {
private int ticket = 100 ;
@Override
public void run ( ) {
while ( true ) {
if ( ticket <= 0 ) {
break ;
} else {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
ticket-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticket + "张票" ) ;
}
}
}
}
卖票出现了问题
问题原因
线程执行的随机性导致的,可能在卖票过程中丢失cpu的执行权,导致出现问题
2.3 卖票案例数据安全问题的解决
为什么出现问题?(这也是我们判断多线程程序是否会有数据安全问题的标准)
如何解决多线程安全问题呢?
怎么实现呢?
把多条语句操作共享数据的代码给锁起来,让任意时刻只能有一个线程执行即可 Java提供了同步代码块的方式来解决
public class Ticket implements Runnable {
private int ticket = 100 ;
private Object obj = new Object ( ) ;
@Override
public void run ( ) {
while ( true ) {
synchronized ( obj) {
if ( ticket <= 0 ) {
break ;
} else {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
ticket-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticket + "张票" ) ;
}
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
Ticket ticket = new Ticket ( ) ;
Thread t1 = new Thread ( ticket) ;
Thread t2 = new Thread ( ticket) ;
Thread t3 = new Thread ( ticket) ;
t1. setName ( "窗口一" ) ;
t2. setName ( "窗口二" ) ;
t3. setName ( "窗口三" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
2.4 同步代码块
synchronized ( 任意对象) {
多条语句操作共享数据的代码
}
synchronized(任意对象):就相当于给代码加锁了,任意对象就可以看成是一把锁
默认情况是打开的,只要有一个线程进去执行代码了,锁就会关闭 当线程执行完出来了,锁才会自动打开 同步的好处和弊端
好处:解决了多线程的数据安全问题 弊端:当线程很多时,因为每个线程都会去判断同步上的锁,这是很耗费资源的,无形中会降低程序的运行效率
public class MyThread extends Thread {
private static int ticketCount = 100 ;
private static Object obj = new Object ( ) ;
@Override
public void run ( ) {
while ( true ) {
synchronized ( obj) {
if ( ticketCount <= 0 ) {
break ;
} else {
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
ticketCount-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticketCount + "张票" ) ;
}
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyThread t1 = new MyThread ( ) ;
MyThread t2 = new MyThread ( ) ;
t1. setName ( "窗口一" ) ;
t2. setName ( "窗口二" ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
2.5 同步方法
修饰符 synchronized 返回值类型 方法名( 方法参数) {
方法体;
}
同步方法的锁对象是什么呢?
静态同步方法
就是把synchronized关键字加到静态方法上
修饰符 static synchronized 返回值类型 方法名( 方法参数) {
方法体;
}
同步静态方法的锁对象是什么呢?
同步代码块和同步方法的区别
同步代码块可以锁住指定代码,同步方法是锁住方法中所有代码 同步代码块可以指定锁对象,同步方法不能指定锁对象
public class MyRunnable implements Runnable {
private static int ticketCount = 100 ;
@Override
public void run ( ) {
while ( true ) {
if ( "窗口一" . equals ( Thread . currentThread ( ) . getName ( ) ) ) {
boolean result = synchronizedMthod ( ) ;
if ( result) {
break ;
}
}
if ( "窗口二" . equals ( Thread . currentThread ( ) . getName ( ) ) ) {
synchronized ( MyRunnable . class ) {
if ( ticketCount == 0 ) {
break ;
} else {
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
ticketCount-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticketCount + "张票" ) ;
}
}
}
}
}
private static synchronized boolean synchronizedMthod ( ) {
if ( ticketCount == 0 ) {
return true ;
} else {
try {
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
ticketCount-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticketCount + "张票" ) ;
return false ;
}
}
}
public class Demo {
public static void main ( String [ ] args) {
MyRunnable mr = new MyRunnable ( ) ;
Thread t1 = new Thread ( mr) ;
Thread t2 = new Thread ( mr) ;
t1. setName ( "窗口一" ) ;
t2. setName ( "窗口二" ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
2.6 Lock锁
虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁, 为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对象Lock
Lock是接口不能直接实例化,这里采用它的实现类ReentrantLock来实例化
方法名 说明 ReentrantLock() 创建一个ReentrantLock的实例
Lock实现提供比使用synchronized方法和语句可以获得更广泛的锁定操作
方法名 说明 void lock() 获得锁 void unlock() 释放锁
import java. util. concurrent. locks. ReentrantLock ;
public class Ticket implements Runnable {
private int ticket = 100 ;
private Object obj = new Object ( ) ;
private ReentrantLock lock = new ReentrantLock ( ) ;
@Override
public void run ( ) {
while ( true ) {
try {
lock. lock ( ) ;
if ( ticket <= 0 ) {
break ;
} else {
Thread . sleep ( 100 ) ;
ticket-- ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "在卖票,还剩下" + ticket + "张票" ) ;
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
} finally {
lock. unlock ( ) ;
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
Ticket ticket = new Ticket ( ) ;
Thread t1 = new Thread ( ticket) ;
Thread t2 = new Thread ( ticket) ;
Thread t3 = new Thread ( ticket) ;
t1. setName ( "窗口一" ) ;
t2. setName ( "窗口二" ) ;
t3. setName ( "窗口三" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
2.7 死锁
概述
线程死锁是指由于两个或者多个线程互相持有对方所需要的资源,导致这些线程处于等待状态,无法前往执行。 什么情况下会产生死锁
public class Demo {
public static void main ( String [ ] args) {
Object objA = new Object ( ) ;
Object objB = new Object ( ) ;
new Thread ( ( ) -> {
while ( true ) {
synchronized ( objA) {
synchronized ( objB) {
System . out. println ( "小康同学正在走路" ) ;
}
}
}
} ) . start ( ) ;
new Thread ( ( ) -> {
while ( true ) {
synchronized ( objB) {
synchronized ( objA) {
System . out. println ( "小薇同学正在走路" ) ;
}
}
}
} ) . start ( ) ;
}
}
3 生产者消费者
3.1 生产者和消费者模式概述
生产者消费者模式是一个十分经典的多线程协作的模式,弄懂生产者消费者问题能够让我们对多线程编程的理解更加深刻 所谓生产者消费者问题,实际上主要是包含了两类线程:
一类是生产者线程用于生产数据 一类是消费者线程用于消费数据 为了解耦生产者和消费者的关系,通常会采用共享的数据区域,就像是一个仓库
生产者生产数据之后直接放置在共享数据区中,并不需要关心消费者的行为 消费者只需要从共享数据区中去获取数据,并不需要关心生产者的行为
3.2 等待和唤醒的方法
方法名 说明 void wait() 导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法 void notify() 唤醒正在等待对象监视器的单个线程 void notifyAll() 唤醒正在等待对象监视器的所有线程
3.3 生产者和消费者案例
案例需求
桌子类(Desk):定义表示包子数量的变量,定义锁对象变量,定义标记桌子上有无包子的变量 生产者类(Cooker):实现Runnable接口,重写run()方法,设置线程任务
判断是否有包子,决定当前线程是否执行 如果有包子,就进入等待状态,如果没有包子,继续执行,生产包子 生产包子之后,更新桌子上包子状态,唤醒消费者消费包子 消费者类(Foodie):实现Runnable接口,重写run()方法,设置线程任务
判断是否有包子,决定当前线程是否执行 如果没有包子,就进入等待状态,如果有包子,就消费包子 消费包子后,更新桌子上包子状态,唤醒生产者生产包子 测试类(Demo):里面有main方法,main方法中的代码步骤如下
public class Desk {
public static boolean flag = false ;
public static int count = 10 ;
public static final Object lock = new Object ( ) ;
}
public class Foodie extends Thread {
@Override
public void run ( ) {
while ( true ) {
synchronized ( Desk . lock) {
if ( Desk . count == 0 ) {
break ;
} else {
if ( Desk . flag) {
System . out. println ( "吃货在吃汉堡包" ) ;
Desk . flag = false ;
Desk . lock. notifyAll ( ) ;
Desk . count-- ;
} else {
try {
Desk . lock. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
}
}
public class Cooker extends Thread {
@Override
public void run ( ) {
while ( true ) {
synchronized ( Desk . lock) {
if ( Desk . count == 0 ) {
break ;
} else {
if ( ! Desk . flag) {
System . out. println ( "厨师正在生产汉堡包" ) ;
Desk . flag = true ;
Desk . lock. notifyAll ( ) ;
} else {
try {
Desk . lock. wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
Foodie f = new Foodie ( ) ;
Cooker c = new Cooker ( ) ;
f. start ( ) ;
c. start ( ) ;
}
}
3.4 生产者和消费者案例优化
需求
将Desk类中的变量,采用面向对象的方式封装起来 生产者和消费者类中构造方法接收Desk类对象,之后在run方法中进行使用 创建生产者和消费者线程对象,构造方法中传入Desk类对象 开启两个线程
public class Desk {
private boolean flag;
private int count;
private final Object lock = new Object ( ) ;
public Desk ( ) {
this ( false , 10 ) ;
}
public Desk ( boolean flag, int count) {
this . flag = flag;
this . count = count;
}
public boolean isFlag ( ) {
return flag;
}
public void setFlag ( boolean flag) {
this . flag = flag;
}
public int getCount ( ) {
return count;
}
public void setCount ( int count) {
this . count = count;
}
public Object getLock ( ) {
return lock;
}
@Override
public String toString ( ) {
return "Desk{" +
"flag=" + flag +
", count=" + count +
", lock=" + lock +
'}' ;
}
}
public class Foodie extends Thread {
private Desk desk;
public Foodie ( Desk desk) {
this . desk = desk;
}
@Override
public void run ( ) {
while ( true ) {
synchronized ( desk. getLock ( ) ) {
if ( desk. getCount ( ) == 0 ) {
break ;
} else {
if ( desk. isFlag ( ) ) {
System . out. println ( "吃货在吃汉堡包" ) ;
desk. setFlag ( false ) ;
desk. getLock ( ) . notifyAll ( ) ;
desk. setCount ( desk. getCount ( ) - 1 ) ;
} else {
try {
desk. getLock ( ) . wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
}
}
public class Cooker extends Thread {
private Desk desk;
public Cooker ( Desk desk) {
this . desk = desk;
}
@Override
public void run ( ) {
while ( true ) {
synchronized ( desk. getLock ( ) ) {
if ( desk. getCount ( ) == 0 ) {
break ;
} else {
if ( ! desk. isFlag ( ) ) {
System . out. println ( "厨师正在生产汉堡包" ) ;
desk. setFlag ( true ) ;
desk. getLock ( ) . notifyAll ( ) ;
} else {
try {
desk. getLock ( ) . wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
}
}
public class Demo {
public static void main ( String [ ] args) {
Desk desk = new Desk ( ) ;
Foodie f = new Foodie ( desk) ;
Cooker c = new Cooker ( desk) ;
f. start ( ) ;
c. start ( ) ;
}
}
3.5 阻塞队列基本使用
阻塞队列继承结构 阻塞队列实现等待唤醒机制
常见BlockingQueue:
ArrayBlockingQueue: 底层是数组,有界 LinkedBlockingQueue: 底层是链表,无界.但不是真正的无界,最大为int的最大值 BlockingQueue的核心方法:
put(anObject): 将参数放入队列,如果放不进去会阻塞 take(): 取出第一个数据,取不到会阻塞
import java. util. concurrent. ArrayBlockingQueue ;
public class test {
public static void main ( String [ ] args) throws Exception {
ArrayBlockingQueue < String > arrayBlockingQueue = new ArrayBlockingQueue < > ( 1 ) ;
arrayBlockingQueue. put ( "汉堡包" ) ;
System . out. println ( arrayBlockingQueue. take ( ) ) ;
System . out. println ( arrayBlockingQueue. take ( ) ) ;
System . out. println ( "程序结束了" ) ;
}
}
3.6 阻塞队列实现等待唤醒机制案例
案例需求
生产者类(Cooker):实现Runnable接口,重写run()方法,设置线程任务
构造方法中接收一个阻塞队列对象 在run方法中循环向阻塞队列中添加包子 打印添加结果 消费者类(Foodie):实现Runnable接口,重写run()方法,设置线程任务
构造方法中接收一个阻塞队列对象 在run方法中循环获取阻塞队列中的包子 打印获取结果 测试类(Demo):里面有main方法,main方法中的代码步骤如下
创建阻塞队列对象 创建生产者线程和消费者线程对象,构造方法中传入阻塞队列对象 分别开启两个线程
import java. util. concurrent. ArrayBlockingQueue ;
public class Cooker extends Thread {
private ArrayBlockingQueue < String > bd;
public Cooker ( ArrayBlockingQueue < String > bd) {
this . bd = bd;
}
@Override
public void run ( ) {
while ( true ) {
try {
bd. put ( "汉堡包" ) ;
System . out. println ( "厨师放入一个汉堡包" ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
import java. util. concurrent. ArrayBlockingQueue ;
public class Foodie extends Thread {
private ArrayBlockingQueue < String > bd;
public Foodie ( ArrayBlockingQueue < String > bd) {
this . bd = bd;
}
@Override
public void run ( ) {
while ( true ) {
try {
String take = bd. take ( ) ;
System . out. println ( "吃货将" + take + "拿出来吃了" ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
import java. util. concurrent. ArrayBlockingQueue ;
public class Demo {
public static void main ( String [ ] args) {
ArrayBlockingQueue < String > bd = new ArrayBlockingQueue < > ( 1 ) ;
Foodie f = new Foodie ( bd) ;
Cooker c = new Cooker ( bd) ;
f. start ( ) ;
c. start ( ) ;
}
}