安全网站建设与服务的关系,网络规划设计师 用途,贺卡制作,软件开发培训班2.线程同步
2.1卖票【应用】 案例需求 某电影院目前正在上映国产大片#xff0c;共有100张票#xff0c;而它有3个窗口卖票#xff0c;请设计一个程序模拟该电影院卖票 实现步骤 定义一个类SellTicket实现Runnable接口#xff0c;里面定义一个成员变量#xff1a;privat…2.线程同步
2.1卖票【应用】 案例需求 某电影院目前正在上映国产大片共有100张票而它有3个窗口卖票请设计一个程序模拟该电影院卖票 实现步骤 定义一个类SellTicket实现Runnable接口里面定义一个成员变量private int tickets 100; 在SellTicket类中重写run()方法实现卖票代码步骤如下 判断票数大于0就卖票并告知是哪个窗口卖的 卖了票之后总票数要减1 票卖没了线程停止 定义一个测试类SellTicketDemo里面有main方法代码步骤如下 创建SellTicket类的对象 创建三个Thread类的对象把SellTicket对象作为构造方法的参数并给出对应的窗口名称 启动线程 代码实现 public class SellTicket implements Runnable {private int tickets 100;//在SellTicket类中重写run()方法实现卖票代码步骤如下Overridepublic void run() {while (true) {if(ticket 0){//卖完了break;}else{try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}ticket--;System.out.println(Thread.currentThread().getName() 在卖票,还剩下 ticket 张票);}}}
}
public class SellTicketDemo {public static void main(String[] args) {//创建SellTicket类的对象SellTicket st new SellTicket();//创建三个Thread类的对象把SellTicket对象作为构造方法的参数并给出对应的窗口名称Thread t1 new Thread(st,窗口1);Thread t2 new Thread(st,窗口2);Thread t3 new Thread(st,窗口3);//启动线程t1.start();t2.start();t3.start();}
}2.2卖票案例的问题【理解】 卖票出现了问题 相同的票出现了多次 出现了负数的票 问题产生原因 线程执行的随机性导致的,可能在卖票过程中丢失cpu的执行权,导致出现问题
2.3同步代码块解决数据安全问题【应用】 安全问题出现的条件 是多线程环境 有共享数据 有多条语句操作共享数据 如何解决多线程安全问题呢? 基本思想让程序没有安全问题的环境 怎么实现呢? 把多条语句操作共享数据的代码给锁起来让任意时刻只能有一个线程执行即可 Java提供了同步代码块的方式来解决 同步代码块格式 synchronized(任意对象) { 多条语句操作共享数据的代码
}synchronized(任意对象)就相当于给代码加锁了任意对象就可以看成是一把锁 同步的好处和弊端 好处解决了多线程的数据安全问题 弊端当线程很多时因为每个线程都会去判断同步上的锁这是很耗费资源的无形中会降低程序的运行效率 代码演示 public class SellTicket implements Runnable {private int tickets 100;private Object obj new Object();Overridepublic void run() {while (true) {synchronized (obj) { // 对可能有安全问题的代码加锁,多个线程必须使用同一把锁//t1进来后就会把这段代码给锁起来if (tickets 0) {try {Thread.sleep(100);//t1休息100毫秒} catch (InterruptedException e) {e.printStackTrace();}//窗口1正在出售第100张票System.out.println(Thread.currentThread().getName() 正在出售第 tickets 张票);tickets--; //tickets 99;}}//t1出来了这段代码的锁就被释放了}}
}public class SellTicketDemo {public static void main(String[] args) {SellTicket st new SellTicket();Thread t1 new Thread(st, 窗口1);Thread t2 new Thread(st, 窗口2);Thread t3 new Thread(st, 窗口3);t1.start();t2.start();t3.start();}
}2.4同步方法解决数据安全问题【应用】 同步方法的格式 同步方法就是把synchronized关键字加到方法上 修饰符 synchronized 返回值类型 方法名(方法参数) { 方法体
}同步方法的锁对象是什么呢? this 静态同步方法 同步静态方法就是把synchronized关键字加到静态方法上 修饰符 static synchronized 返回值类型 方法名(方法参数) { 方法体
}同步静态方法的锁对象是什么呢? 类名.class 代码演示 public class MyRunnable implements Runnable {private static int ticketCount 100;Overridepublic 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.5Lock锁【应用】
虽然我们可以理解同步代码块和同步方法的锁对象问题但是我们并没有直接看到在哪里加上了锁在哪里释放了锁为了更清晰的表达如何加锁和释放锁JDK5以后提供了一个新的锁对象Lock
Lock是接口不能直接实例化这里采用它的实现类ReentrantLock来实例化 ReentrantLock构造方法 方法名说明ReentrantLock()创建一个ReentrantLock的实例 加锁解锁方法 方法名说明void lock()获得锁void unlock()释放锁 代码演示 public class Ticket implements Runnable {//票的数量private int ticket 100;private Object obj new Object();private ReentrantLock lock new ReentrantLock();Overridepublic void run() {while (true) {//synchronized (obj){//多个线程必须使用同一把锁.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.6死锁【理解】 概述 线程死锁是指由于两个或者多个线程互相持有对方所需要的资源导致这些线程处于等待状态无法前往执行 什么情况下会产生死锁 资源有限同步嵌套 代码演示 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();}
}