JavaSE基础(98) 线程同步完成售票实例的6种方式

自定义线程和线程同步原理不懂的可以先看下面2个链接资料

Java之自定义线程的2种方式:https://blog.csdn.net/qq_38225558/article/details/82118862

Java之线程安全问题的3种处理方式(通过线程同步):https://blog.csdn.net/qq_38225558/article/details/82154321

 

线程同步:完成多线程售票实例   ==>  假设存在2个窗口售票

第1种方式:自定义线程继承Thread类     同步方法 ==> 使用synchronized 修饰方法  

public class Demo1{
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread t1 = new TicketThread(station);
		TicketThread t2 = new TicketThread(station);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
    //定义一个买票的方法
	public synchronized void getTicket(){
		if(tickets>0){
			System.out.println("车票还剩: "+ (--tickets) + "张 !");
		}
	}
}
class TicketThread extends Thread{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

运行结果图:


第2种方式:自定义线程继承Thread类    同步代码块  synchronized(被加锁的对象){ 代码 }

public class Demo2{
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread t1 = new TicketThread(station);
		TicketThread t2 = new TicketThread(station);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
	//定义一个买票的方法
	public void getTicket(){
		synchronized (this) {
			if(tickets>0){
				System.out.println("车票还剩: "+ (--tickets) + "张 !");
			}
		}
	}
}
class TicketThread extends Thread{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

第3种方式:自定义线程继承Thread类    锁机制lock

public class Demo3 {
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread t1 = new TicketThread(station);
		TicketThread t2 = new TicketThread(station);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
	// 1.创建Lock实现类的对象  注意:需在getTicket()方法外执行,如果在getTicket()方法里执行,在getTicket()方法被调用的时候就会被创建很多ReentrantLock对象,即出现很多锁
	ReentrantLock lock = new ReentrantLock();
	//定义一个买票的方法
	public void getTicket(){
		// 2.使用Lock对象的lock方法加锁
		lock.lock();
		if(tickets>0){
			System.out.println("车票还剩: "+ (--tickets) + "张 !");
		}
		// 3.使用Lock对象的unlock方法解锁
		lock.unlock();
	}
}
class TicketThread extends Thread{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

第4种方式:自定义线程 实现Runnable接口    锁机制lock

public class Demo4 {
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread ticketThread1 = new TicketThread(station);
		TicketThread ticketThread2 = new TicketThread(station);
		Thread t1 = new Thread(ticketThread1);
		Thread t2 = new Thread(ticketThread2);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
	// 1.创建Lock实现类的对象  注意:需在getTicket()方法外执行,如果在getTicket()方法里执行,在getTicket()方法被调用的时候就会被创建很多ReentrantLock对象,即出现很多锁
	ReentrantLock lock = new ReentrantLock();
	//定义一个买票的方法
	public void getTicket(){
		// 2.使用Lock对象的lock方法加锁
		lock.lock();
		if(tickets>0){
			System.out.println("车票还剩: "+ (--tickets) + "张 !");
		}
		// 3.使用Lock对象的unlock方法解锁
		lock.unlock();
	}
}
class TicketThread implements Runnable{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

第5种方式:自定义线程 实现Runnable接口     同步方法 ==> 使用synchronized 修饰方法 

public class Demo5 {
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread ticketThread1 = new TicketThread(station);
		TicketThread ticketThread2 = new TicketThread(station);
		Thread t1 = new Thread(ticketThread1);
		Thread t2 = new Thread(ticketThread2);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
	//定义一个买票的方法
	public synchronized  void getTicket(){
		if(tickets>0){
			System.out.println("车票还剩: "+ (--tickets) + "张 !");
		}
	}
}
class TicketThread implements Runnable{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

第6种方式:自定义线程 实现Runnable接口     同步代码块  synchronized(被加锁的对象){ 代码 }

public class Demo6 {
	public static void main(String[] args) {
		Station station = new Station();
		TicketThread ticketThread1 = new TicketThread(station);
		TicketThread ticketThread2 = new TicketThread(station);
		Thread t1 = new Thread(ticketThread1);
		Thread t2 = new Thread(ticketThread2);
		t1.start();
		t2.start();
	}
}
class Station{
	private static int tickets = 20;//记录车票的数量   使用static目的:确保2个窗口是共同在出售这20张票而不是这2个窗口各自出售20张 共出售40张
	//定义一个买票的方法
	public void getTicket(){
		synchronized (this) {
			if(tickets>0){
				System.out.println("车票还剩: "+ (--tickets) + "张 !");
			}
		}
	}
}
class TicketThread implements Runnable{
	private Station station;
	public TicketThread(Station station) {
		this.station = station;
	}
	@Override
	public void run() {
		while(true){
			station.getTicket();
		}	
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郑清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值