一、多线程编程模板
线程 操作 资源类
高内聚 低耦合
二、实现步骤
1、创建资源类
2、资源类里创建同步方法、同步代码块
三、12306卖票程序
3.1、synchronized实现
3.1.1、Ticket
/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 8:54* @Description: 资源类火车票*/
public class Ticket {private Integer num = 30;public synchronized void saleTicket() {if (num > 0) {System.out.println(Thread.currentThread().getName() + " 卖出第:" + (num--) + "张票,还剩下:" + num + "张票!");}}}
3.1.2、SaleTicketSynchronizedMainApp
/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 8:57* @Description: 需求:三个售票员 卖出 30张票(synchronized实现)* 多线程编程的企业级套路 + 模板:在高内聚低耦合的前提下,线程操作资源类*/
public class SaleTicketSynchronizedMainApp {public static void main(String[] args) {Ticket ticket = new Ticket();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "A").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "B").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "C").start();}}
3.1.3、结果
3.2、Lock实现
3.2.1、Ticket
/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 8:57* @Description: 需求:三个售票员 卖出 30张票(synchronized实现)* 多线程编程的企业级套路 + 模板:在高内聚低耦合的前提下,线程操作资源类*/
public class SaleTicketSynchronizedMainApp {public static void main(String[] args) {Ticket ticket = new Ticket();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "A").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "B").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "C").start();}}
3.2.2、SaleTicketLockMainApp
/*** @Author : 一叶浮萍归大海* @Date: 2023/11/20 8:57* @Description: 需求:三个售票员 卖出 30张票(Lock实现)* 多线程编程的企业级套路 + 模板:在高内聚低耦合的前提下,线程操作资源类*/
public class SaleTicketLockMainApp {public static void main(String[] args) {Ticket ticket = new Ticket();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "AA").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "BB").start();new Thread(() -> {for (int i = 1; i <= 40; i++) {ticket.saleTicket();}}, "CC").start();}}
3.2.3、结果
四、Lock
4.1、概述
4.2、Lock接口的实现类ReentrantLock
4.3、如何使用ReentrantLock
class X {private final ReentrantLock lock = new ReentrantLock();// ...public void m() {lock.lock(); // block until condition holdstry {// ... method body} finally {lock.unlock()}}}
4.4、synchronized VS Lock
(1)首先synchronized是java内置的关键字,在jvm层面,Lock是个java类;
(2)synchronized无法判断是否获取锁的状态,Lock可以判断是否获取到锁;
(3)synchronized会自动释放锁(a 线程执行完同步代码会释放锁 ;b 线程执行过程中发生异常会释放锁),Lock需在finally中手工释放锁(unlock()释放锁),否则容易造成线程死锁;
(4)用synchronized关键字的两个线程1和线程2,如果当前线程1获得锁,线程2等待。如果线程1阻塞,线程2则会一直等待下去,而Lock锁就不一定会等待下去,如果尝试获取不到锁, 线程可以不用一直等待就结束了;
(5)synchronized的锁可重入、不可中断、非公平,而Lock锁可重入、可中断、可公平(两者皆可);
(6)Lock锁适合大量同步的代码的同步问题,synchronized锁适合代码少量的同步问题;