java 线程同步--卖票问题

 

线程同步方法:

(1)、同步代码块,格式:

 

synchronized (同步对象){

//同步代码

}

(2)、同步方法,格式:

在方法前加synchronized修饰

 

问题:

多个人同时买票。

 

1、资源没有同步。

 

package thread;

public class Tickets implements Runnable {

	private int count = 5;

	@Override
	public void run() {
		for (int i = 0; i < 10; ++i) {
			if (count > 0) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(count--);
			}
		}
	}
	
	public static void main(String[] args) {
		Tickets tickets=new Tickets();
        Thread t1=new Thread(tickets);
        Thread t2=new Thread(tickets);
        Thread t3=new Thread(tickets);
        t1.start();
        t2.start();
        t3.start();
    }

}


运行结果:

 

 

5
3
4
2
2
1

 

很明显结果是错的。

2、同步代码块

run方法中进行同步,也就是对共享资源(票数、count)进行同步

 

package thread;

public class Tickets implements Runnable {

	private int count = 5;

	@Override
	public void run() {
		for (int i = 0; i < 10; ++i) {
			synchronized (this) {
				if (count > 0) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(count--);
				}
			}
		}
	}

	public static void main(String[] args) {
		Tickets tickets = new Tickets();
		Thread t1 = new Thread(tickets);
		Thread t2 = new Thread(tickets);
		Thread t3 = new Thread(tickets);
		t1.start();
		t2.start();
		t3.start();
	}

}

 

3、同步方法

 

 

package thread;

public class Tickets implements Runnable {

	private int count = 5;

	@Override
	public void run() {
		for (int i = 0; i < 10; ++i) {
			sale();
		}
	}

	public synchronized void sale() {
		if (count > 0) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(count--);
		}
	}

	public static void main(String[] args) {
		Tickets tickets = new Tickets();
		Thread t1 = new Thread(tickets);
		Thread t2 = new Thread(tickets);
		Thread t3 = new Thread(tickets);
		t1.start();
		t2.start();
		t3.start();
	}

}

 

 

 

 

 

 

参考:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html 。特此声明。

 

 

 

 

 

 

Java中,当有多线程同时尝试访问并修改共享资源,比如三个窗口卖票这个场景,如果不进行适当的同步控制,可能会导致数据竞争、死锁等问题。为了确保公平性和一致性,可以使用synchronized关键字或者Lock接口来实现线程同步。 这里是一个简单的例子,假设我们有三个Window类,每个窗口都有ticketCount变量表示剩余票数: ```java class Window { private int ticketCount = 100; private final Object lock; // 使用锁对象防止并发 public Window() { this.lock = new Object(); } synchronized void sellTicket(int num) { if (ticketCount >= num) { ticketCount -= num; System.out.println(Thread.currentThread().getName() + " sold " + num + " tickets, remaining: " + ticketCount); } else { System.out.println(Thread.currentThread().getName() + " cannot sell more tickets, not enough."); } notifyAll(); // 如果票足够,唤醒其他等待的线程 } public synchronized void waitForTicket() throws InterruptedException { while (ticketCount == 0) { wait(lock); // 等待,直到票数充足 } } } public class TicketSale { public static void main(String[] args) { Window window1 = new Window(); Window window2 = new Window(); Window window3 = new Window(); Thread t1 = new Thread(() -> { window1.waitForTicket(); window1.sellTicket(50); }); Thread t2 = new Thread(() -> { window2.waitForTicket(); window2.sellTicket(30); }); Thread t3 = new Thread(() -> { window3.waitForTicket(); window3.sellTicket(20); }); t1.start(); t2.start(); t3.start(); } } ``` 在这个例子中,每个窗口都包含一个synchronized的sellTicket方法和waitForTicket方法,这样当一个线程获取到票(sellTicket)时,会先检查票是否足够,然后减票并通知其他等待的线程。如果票不够,则让出锁等待(waitForTicket)。通过这种方式,多个线程能够安全地协作卖票
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值