从身份证号码里提取生日和周岁的方法
517
2022-05-29
模拟买票练习多线程
使用Thread和Runable练习买票和练习共享资源问题
方式一:继承Thread类
1.首先定义一个ticks变量为总票数为10
1
2.定义run方法,循环的扣除票数量(简单写一下打印ticks票数)
1
3.模拟四个窗口购买票
1
问题1:打印的结果是每个窗口都打印了10张票,也就是说变量tickets没有共享
解决1:通过给变量添加static,使变量变为共享变量如(代码1)
解决2:通过实现runable接口解决共享变量问题(代码2)
问题2:打印的ticks数量顺序不是从10->1,顺序乱了,也就是数据不同步
解决:通过代码块同步或者方法同步解决问题(加锁)(代码3、4)
代码1:
package com.lzw.java_thread.tickets;/** * 使用Thread和Runable练习买票和练习共享资源问题 * 方式一:继承Thread类 * 1.首先定义一个ticks变量为总票数为10 * 2.定义run方法,循环的扣除票数量(简单写一下打印ticks票数) * 3.模拟四个窗口购买票 */public class ThreadTicks extends Thread{ private static int tickets = 10; @Override public void run() { while (tickets>0){ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this){ if (tickets>0){ System.out.println(Thread.currentThread().getName() + " 剩余" + (tickets--) + "张票"); } } } } public static void main(String[] args) { ThreadTicks tt1 = new ThreadTicks(); ThreadTicks tt2 = new ThreadTicks(); ThreadTicks tt3 = new ThreadTicks(); ThreadTicks tt4 = new ThreadTicks(); tt1.start(); tt2.start(); tt3.start(); tt4.start(); }}1234567891011121314151617181920212223242526272829303132333435363738
代码2:
package com.lzw.java_thread.tickets;/** *第二种方式: * 1.实现Runable接口 * 2.创建四个线程都传入一个Runable子类,实现对象资源共享 * 3.调用start()方法 */public class RunableTickets implements Runnable{ private int tickets = 10; @Override public void run() { while (tickets>0){ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if (tickets>0){ System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票"); } } } public static void main(String[] args) { RunableTickets rtt = new RunableTickets(); Thread td1 = new Thread(rtt); Thread td2 = new Thread(rtt); Thread td3 = new Thread(rtt); Thread td4 = new Thread(rtt); td1.start(); td2.start(); td3.start(); td4.start(); }}123456789101112131415161718192021222324252627282930313233343536
代码3:
package com.lzw.java_thread.tickets;/** * @Auther: lzw * 使用同步代码块,实现共享资源同步 */public class RunableTickets1 implements Runnable{ private int tickets = 10; @Override public void run() { while (tickets>0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this){ if (tickets>0){ System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票"); } } } } public static void main(String[] args) { RunableTickets1 rtt = new RunableTickets1(); Thread td1 = new Thread(rtt); Thread td2 = new Thread(rtt); Thread td3 = new Thread(rtt); Thread td4 = new Thread(rtt); td1.start(); td2.start(); td3.start(); td4.start(); }}12345678910111213141516171819202122232425262728293031323334353637
代码4:
package com.lzw.java_thread.tickets;/** * @Auther: lzw * 使用同步方法,实现共享资源同步 */public class RunableTickets2 implements Runnable{ private int tickets = 10; @Override public void run() { while (tickets>0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.sale(); } } public synchronized void sale(){ if (tickets>0){ System.out.println(Thread.currentThread().getName()+" 剩余"+(tickets--)+"张票"); } } public static void main(String[] args) { RunableTickets2 rtt = new RunableTickets2(); Thread td1 = new Thread(rtt); Thread td2 = new Thread(rtt); Thread td3 = new Thread(rtt); Thread td4 = new Thread(rtt); td1.start(); td2.start(); td3.start(); td4.start(); }}
任务调度
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。