小言_互联网的博客

【java】多线程_并发_同步_快乐影院

351人阅读  评论(0)

同步块版

import java.util.ArrayList;
import java.util.List;

/**
 * 快乐影院
 * @author gudaochangsheng
 *
 */
public class HappyCinema2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//可用位置
		List<Integer> available = new ArrayList<Integer>();
		available.add(1);
		available.add(2);
		available.add(3);
		available.add(4);
		available.add(5);
		available.add(6);
		available.add(7);
		//顾客需要位置
		List<Integer> seats1 = new ArrayList<Integer>();
		seats1.add(1);
		seats1.add(2);
		List<Integer> seats2 = new ArrayList<Integer>();
		seats2.add(4);
		seats2.add(5);
		seats2.add(6);
		GdcsCinema c = new GdcsCinema(available, "happy gdcs");
		new Thread(new HappyCustomer(c, seats1),"a").start();
		new Thread(new HappyCustomer(c, seats2),"b").start();
	}

}
class HappyCustomer implements Runnable
{
	GdcsCinema cinema;
	List<Integer> seats;
	public HappyCustomer(GdcsCinema cinema, List<Integer>seats) {
		this.cinema = cinema;
		this.seats = seats;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		synchronized (cinema) {
			boolean flag = cinema.bookTickets(seats);
			if(flag)
			{
				System.out.println("出票成功"+Thread.currentThread().getName()+"-<位置为:"+seats);
			}
			else
			{
				System.out.println("出票失败"+Thread.currentThread().getName()+"-<位置不够:");
			}
		}
	}
	
}
class GdcsCinema{
	List<Integer> available;//可用的位置
	String name;//名称
	public GdcsCinema(List<Integer> available, String name) {
		this.available = available;
		this.name = name;
	}
	public boolean bookTickets(List<Integer> seats)
	{
		System.out.println("可用位置为:"+available);
		List<Integer> copy = new ArrayList<Integer>();
		copy.addAll(available);
		//相减
		copy.removeAll(seats);
		//判断大小
		if(available.size()-copy.size()!=seats.size())
		{
			return false;
		}
		//成功
		available = copy;
		return true;
	}
}

运行效果:

同步方法版

/** 

* @author 作者 gudaochangsheng

* @version 创建时间:2019年10月4日 下午8:25:14 

* 类说明 

*/

public class Happy12306 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Web12306 c = new Web12306(8, "happy gdcs");
		new Passenger(c, "a", 2).start();
		new Passenger(c, "b", 1).start();
	}

}

class Passenger extends Thread
{
	int seats;
	public Passenger(Runnable target,String name,int seats) {
		super(target,name);
		this.seats = seats;
	}
}
class Web12306 implements Runnable{
	int available;//可用的位置
	String name;//名称
	public Web12306(int available, String name) {
		this.available = available;
		this.name = name;
	}
	
	public void run() {
			Passenger p = (Passenger)Thread.currentThread();
			boolean flag = this.bookTickets(p.seats);
			if(flag)
			{
				System.out.println("出票成功"+Thread.currentThread().getName()+"-<位置为:"+p.seats);
			}
			else
			{
				System.out.println("出票失败"+Thread.currentThread().getName()+"-<位置不够:");
			}
		}
	public synchronized boolean bookTickets(int seats)
	{
		System.out.println("可用位置为:"+available);
		if(seats>available)
		{
			return false;
		}
		available-=seats;
		return true;
	}
}


运行效果:


转载:https://blog.csdn.net/wl1780852311/article/details/102082860
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场