Java多线程的~~~synchronized加入参数,以实现独立片段

有时候,我们不希望在整个方法的前面加上synchronized这个keyword。这将使整个方法调用变得缓慢,我们只是重点

代码的地方添加这个synchronized这个keyword,然后这样就能加快方法或者代码的运行效率。

然后可能另一种情况就

是,我们有两个变量,当中不论什么一个变量都是同一时候仅仅能一个变量訪问,可是两个变量能够在同一时候被两个变量訪问。这样的

需求下我们就得使用sychronized的带參数的方法来实现这样的需求。


以下我们来看一个样例,一个模拟电影院售票的样例

package com.bird.concursey.charpet2;

public class Cinema {
	
	private long vacanciesCinema1;
	
	private long vacanciesCinema2;
	
	private final Object controlCinema1 = new Object();
	
	private final Object controlCinema2 = new Object();
	
	public Cinema() {
		vacanciesCinema1 = 20;
		vacanciesCinema2 = 20;
	}
	
	public boolean sellTickets1(int number) {
		synchronized (controlCinema1) {
			if(number < vacanciesCinema1) {
				vacanciesCinema1 -= number;
				return true;
			}else{
				return false;
			}
		}
	}
	
	public boolean sellTickets2(int number) {
		synchronized (controlCinema2) {
			if(number < vacanciesCinema2) {
				vacanciesCinema2 -= number;
				return true;
			}else{
				return false;
			}
		}
	}
	
	
	public boolean returnTicket1(int number) {
		synchronized (controlCinema1) {
			vacanciesCinema1 += number;
			return true;
		}
	}
	
	public boolean returnTicket2(int number) {
		synchronized (controlCinema2) {
			vacanciesCinema2 += number;
			return true;
		}
	}

	public long getVacanciesCinema1() {
		return vacanciesCinema1;
	}

	public void setVacanciesCinema1(long vacanciesCinema1) {
		this.vacanciesCinema1 = vacanciesCinema1;
	}

	public long getVacanciesCinema2() {
		return vacanciesCinema2;
	}

	public void setVacanciesCinema2(long vacanciesCinema2) {
		this.vacanciesCinema2 = vacanciesCinema2;
	}
}



以下是实际售票的类


package com.bird.concursey.charpet2;

public class TicketOffice1 implements Runnable {
	
	private Cinema cinema;
	
	public TicketOffice1(Cinema cinema) {
		this.cinema = cinema;
	}

	@Override
	public void run() {
		cinema.sellTickets1(3);
		cinema.sellTickets1(2);
		cinema.sellTickets2(2);
		cinema.returnTicket1(3);
		cinema.sellTickets1(5);
		cinema.sellTickets2(2);
		cinema.sellTickets2(2);
		cinema.sellTickets2(2);
	}

}



package com.bird.concursey.charpet2;

public class TicketOffice2 implements Runnable {

	private Cinema cinema;

	public TicketOffice2(Cinema cinema) {
		this.cinema = cinema;
	}

	@Override
	public void run() {
		cinema.sellTickets2(2);
		cinema.sellTickets2(4);
		cinema.sellTickets1(2);
		cinema.sellTickets1(1);
		cinema.returnTicket2(2);
		cinema.sellTickets1(3);
		cinema.sellTickets2(2);
		cinema.sellTickets1(2);
	}

	public static void main(String[] args) {
		Cinema cinema = new Cinema();
		TicketOffice1 ticketOffice1 = new TicketOffice1(cinema);
		Thread thread1 = new Thread(ticketOffice1, "TicketOffice1");

		TicketOffice2 ticketOffice2 = new TicketOffice2(cinema);
		Thread thread2 = new Thread(ticketOffice2, "TicketOffice2");

		thread1.start();
		thread2.start();

		try {
			thread1.join();
			thread2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.printf("Room 1 Vacancies: %d\n",cinema.getVacanciesCinema1());
		System.out.printf("Room 2 Vacancies: %d\n",cinema.getVacanciesCinema2());
	}
}

这里能够看到,每次执行都会是期望的效果,并且通过单例模式。我们非常好的控制了两个属性的訪问顺序,非常好的实

现在我们的需求。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-12 10:32  mfrbuaa  阅读(1232)  评论(0编辑  收藏  举报