模拟买票练习多线程
使用Thread和Runable练习买票和练习共享资源问题
方式一:继承Thread类
1.首先定义一个ticks变量为总票数为10
2.定义run方法,循环的扣除票数量(简单写一下打印ticks票数)
3.模拟四个窗口购买票
问题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();
}}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)