当前位置: 首页 > news >正文

广元网页制作公司seo网站优化经理

广元网页制作公司,seo网站优化经理,企业建站报价方案,网站建设公司文案一、卖票的多线程实现 需求#xff1a;共有100张票#xff0c;而它有3个窗口卖票#xff0c;请设计一个程序模拟该电影院卖票 代码实现#xff1a; /*** Author#xff1a;kkoneone11* name#xff1a;SellTicket1* Date#xff1a;2023/8/26 11:32*/ public class S… 一、卖票的多线程实现 需求共有100张票而它有3个窗口卖票请设计一个程序模拟该电影院卖票 代码实现 /*** Authorkkoneone11* nameSellTicket1* Date2023/8/26 11:32*/ public class SellTicket1 implements Runnable{private int tickets 100;Overridepublic void run() {while(true){if(tickets 0){break;}else {try{Thread.sleep(100);}catch (Exception e){e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 票数还剩余 tickets);}}} }public class SellTicketDemo {public static void main(String[] args) {SellTicket1 st new SellTicket1();Thread thread1 new Thread(st, 窗口1);Thread thread2 new Thread(st, 窗口2);Thread thread3 new Thread(st, 窗口3);thread1.start();thread2.start();thread3.start();} }可以看到这种程序写法的问题有 相同的票出现了多次 出现了负数的票   问题产生的原因分析这种多线程共享的是同一份数据线程执行的随机性导致的,可能在卖票过程中丢失cpu的执行权,导致出现问题 二、解决问题的方案 要解决这个问题实际上就是让程序没有安全问题如何实现其实就是让每次操作的时候只能有一个线程执行成功即可那么可以实现的方案如下 同步代码块 实现方法 synchronized(任意对象) { 多条语句操作共享数据的代码 } 优缺点 好处解决了多线程的数据安全问题 弊端当线程很多时因为每个线程都会去判断同步上的锁这是很耗费资源的无形中会降低程序的运行效率 实例 public class SellTicket1 implements Runnable{private int tickets 100;private Object obj new Object();Overridepublic void run() {while(true){synchronized (obj){//当线程进来的时候就会把这段代码锁起来if(tickets 0){break;}else {try{Thread.sleep(100);}catch (Exception e){e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 票数还剩余 tickets);}}//到此处锁就会释放了}} }public class SellTicketDemo {public static void main(String[] args) {SellTicket1 st new SellTicket1();Thread thread1 new Thread(st, 窗口1);Thread thread2 new Thread(st, 窗口2);Thread thread3 new Thread(st, 窗口3);thread1.start();thread2.start();thread3.start();} } 同步方法 实现方法 锁住的对象是this 修饰符 synchronized 返回值类型 方法名(方法参数) { 方法体 } 静态同步方法 实现方法 锁住的对象是类名.class 修饰符 static synchronized 返回值类型 方法名(方法参数) { 方法体 } 实例 public class SellTicket1 implements Runnable{private static int tickets 100;Overridepublic void run() {while(true){if(窗口一.equals(Thread.currentThread().getName())){//同步方法boolean b synchronizedMthod();if(b){break;}}else if(窗口二.equals(Thread.currentThread().getName())){//同步代码块synchronized (SellTicket1.class){if(tickets 0){break;}else{try{Thread.sleep(100);}catch (Exception e){e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 票数还剩余 tickets);}}}}}private static synchronized boolean synchronizedMthod(){if(tickets 0){return true;}else{try{Thread.sleep(100);}catch (Exception e){e.printStackTrace();}tickets--;System.out.println(Thread.currentThread().getName() 票数还剩余 tickets);return false;}} }public class SellTicketDemo {public static void main(String[] args) {SellTicket1 st new SellTicket1();Thread thread1 new Thread(st, 窗口1);Thread thread2 new Thread(st, 窗口2);Thread thread3 new Thread(st, 窗口3);thread1.start();thread2.start();thread3.start();} } 总结 无论synchronized关键字加在方法上还是对象上如果它作用的对象是非静态的则它取得的锁是对象如果synchronized作用的对象是一个静态方法或一个类则它取得的锁是对类该类所有的对象同一把锁。 ReentrantLock() 如果我们想可以直接看到在哪里加上了锁在哪里释放了锁为了更清晰的表达如何加锁和释放锁JDK5以后提供了一个新的锁对象Lock 方法名说明void lock()获得锁void unlock()释放锁 实例 public class SellTicket1 implements Runnable{//票的数量private int tickets 100;private Object obj new Object();private ReentrantLock lock new ReentrantLock();Overridepublic void run() {while (true) {//synchronized (obj){//多个线程必须使用同一把锁.try {lock.lock();if (tickets 0) {//卖完了break;} else {Thread.sleep(100);tickets--;System.out.println(Thread.currentThread().getName() 在卖票,还剩下 tickets 张票);}} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}// }}} }public class SellTicketDemo {public static void main(String[] args) {SellTicket1 st new SellTicket1();Thread thread1 new Thread(st, 窗口1);Thread thread2 new Thread(st, 窗口2);Thread thread3 new Thread(st, 窗口3);thread1.start();thread2.start();thread3.start();} }
http://www.zqtcl.cn/news/950980/

相关文章:

  • 图书馆网站建设需求方案企业网站背景图片
  • 网站app的作用做家教网站资质
  • 资源网站搭建北京app网站建设
  • 天津做宠物饲料的网站wordpress添加注册页面模板
  • 网站建设公司石家庄php网站开发报价
  • 国外免费网站服务器链接cpa自己做网站
  • 手机网站的优缺点2024中国企业500强
  • 黄河道网站建设网站设计标语
  • 企业网站建设范文wordpress 5.1
  • 网站 河北 备案 慢设计一个营销方案
  • 网站建设培训合肥品牌设计案例
  • 建网站注册免费云服务器
  • 可以做网站的公司有哪些聊天软件开发厂家有哪些
  • 正规网站建设公司一般要多少钱婚纱网站有哪些
  • 企业网站开发目的和意义住房和城乡建设厅官网查询
  • 直播一级a做爰片免费网站wordpress 模板 使用
  • 网站开发中期检查优质的菏泽网站建设
  • 建设网站号码在线html编辑
  • 品牌型网站制作有哪些公司石家庄广告制作公司
  • 做网站赚几百万网站效果图怎么做的
  • 哪些网站做企业招聘不要花钱wordpress底部导航代码
  • 怎么用链接进自己做的网站企业组织架构
  • 建设新网站征求意见网站设计佛山
  • 重庆建设造价工程信息网站东莞电商页面设计公司
  • 乔拓云智能建站官网登录入口怎么样做网站卖农产品
  • 怎么维护好网站网站的域名每年都要续费
  • 运动网站模板佛山三水区有没有网站建设公司
  • 申请微官网的网站国外域名注册商网站
  • 集团公司网站建设建设中学校园网站的来源
  • 产品展示网站含后台网站模板下载网站开发什么语言好