当前位置: 首页 > 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.pierceye.com/news/950980/

相关文章:

  • 精品课程网站开发平台福建省建设厅网站 保证金
  • 网站后台 不能删除文章贵州建设厅网站首页
  • 重庆市园林建设有限公司网站酒店平台网站建设
  • c 网站开发实例教程超级外链工具 增加外链中
  • ip怎么做网站外贸网站建设哪里好
  • 市网站建设网站排名查询alexa
  • 西安建设网站首页网络互联网推广
  • 百度搜索网站显示图片wordpress 工作室
  • 网站页面模板 建设中集团做网站优势
  • 提供佛山网站制作大连市建设工程集团有限公司
  • 北京网站设计外包公司价格网站怎么备案在哪里
  • 视频网站广告代码网站建设怎么插图片
  • 网站建设需要敲代码吗外贸网站商城
  • wordpress增加网站网页关键词企业网站的需求是什么
  • 口碑好网站建设电话什么是搜索引擎优化用一句话概括
  • wordpress的vps建站流程ui是什么意思
  • 广州专业的网站建设公司哪家好wordpress博客优秀
  • 没有服务器建网站免费开发软件制作平台
  • 高端品牌网站建设明细报价报青岛市公共资源交易网
  • 余姚 网站建设济南网站开发薪酬
  • 一流高职院校建设工作网站野花香社区在线观看播放
  • 大连建设科技网站绿色主题 wordpress
  • 外网访问不了内网建设的网站做网站电销
  • 桂平市住房和城乡建设局网站杭州app定制公司
  • 免费做金融网站自己做网站想更换网址
  • 浙江省网站建设公司成都五月花网页设计培训
  • 江苏外贸网站建设网站开发工作流审批流
  • 自媒体论坛交流推荐网站外部优化
  • 南宁网络建站直接用源码做网站盗版吗
  • 哪些网站可以做驾考试题旅游网站排名排行榜