当你使用synchronized关键字去保护一个代码块时,你必须传入一个对象的引用。

正常来讲,你讲使用this关键字去引用执行这个方法的对象,但是你可以使用其他对象的引用。

通常的,这些对象将会是专有的。例如,如果多个线程共享一个类中有2个独立的属性,你必须对每个变量做读取同步操作,

但是如果一个线程读取一个属性而另一个线程读取另一个这个没问题的。

本例中,你讲学会如何解决这个问题。我们将模拟带有2块屏幕和2个售票窗口的电影院。

当一个售票窗口买票时,它卖出的肯定是其中某个播放厅的座位,这样一来每个厅的座位数就是独立的属性。

Cinema.java
package com.dylan.thread.ch2.c02.task;

public class Cinema {

	/**
* This two variables store the vacancies in two cinemas
*/
private long vacanciesCinema1;
private long vacanciesCinema2; /**
* Two objects for the synchronization. ControlCinema1 synchronizes the
* access to the vacancesCinema1 attribute and controlCinema2 synchronizes
* the access to the vacanciesCinema2 attribute.
*/
private final Object controlCinema1, controlCinema2; /**
* Constructor of the class. Initializes the objects
*/
public Cinema(){
controlCinema1=new Object();
controlCinema2=new Object();
vacanciesCinema1=20;
vacanciesCinema2=20;
} /**
* This method implements the operation of sell tickets for the cinema 1
* @param number number of tickets sold
* @return true if the tickets are sold, false if there is no vacancies
*/
public boolean sellTickets1 (int number) {
synchronized (controlCinema1) {
if (number<vacanciesCinema1) {
vacanciesCinema1-=number;
return true;
} else {
return false;
}
}
} /**
* This method implements the operation of sell tickets for the cinema 2
* @param number number of tickets sold
* @return true if the tickets are sold, false if there is no vacancies
*/
public boolean sellTickets2 (int number){
synchronized (controlCinema2) {
if (number<vacanciesCinema2) {
vacanciesCinema2-=number;
return true;
} else {
return false;
}
}
} /**
* This method implements the operation of return tickets for the cinema 1
* @param number number of the tickets returned
* @return true
*/
public boolean returnTickets1 (int number) {
synchronized (controlCinema1) {
vacanciesCinema1+=number;
return true;
}
} /**
* This method implements the operation of return tickets for the cinema 1
* @param number number of the tickets returned
* @return true
*/
public boolean returnTickets2 (int number) {
synchronized (controlCinema2) {
vacanciesCinema2+=number;
return true;
}
} /**
* Return the vacancies in the cinema 1
* @return the vacancies in the cinema 1
*/
public long getVacanciesCinema1() {
return vacanciesCinema1;
} /**
* Return the vacancies in the cinema 2
* @return the vacancies in the cinema 2
*/
public long getVacanciesCinema2() {
return vacanciesCinema2;
} }

TicketOffice1.java
package com.dylan.thread.ch2.c02.task;

/**
* This class simulates a ticket office. It sell or return tickets
* for the two cinemas
*
*/
public class TicketOffice1 implements Runnable { /**
* The cinema
*/
private Cinema cinema; /**
* Constructor of the class
* @param cinema the cinema
*/
public TicketOffice1 (Cinema cinema) {
this.cinema=cinema;
} /**
* Core method of this ticket office. Simulates selling and returning tickets
*/
@Override
public void run() {
cinema.sellTickets1(3);
cinema.sellTickets1(2);
cinema.sellTickets2(2);
cinema.returnTickets1(3);
cinema.sellTickets1(5);
cinema.sellTickets2(2);
cinema.sellTickets2(2);
cinema.sellTickets2(2);
} }
TicketOffice2
package com.dylan.thread.ch2.c02.task;

/**
* This class simulates a ticket office. It sell or return tickets
* for the two cinemas
*
*/
public class TicketOffice2 implements Runnable { /**
* The cinema
*/
private Cinema cinema; /**
* Constructor of the class
* @param cinema the cinema
*/
public TicketOffice2(Cinema cinema){
this.cinema=cinema;
} /**
* Core method of this ticket office. Simulates selling and returning tickets
*/
@Override
public void run() {
cinema.sellTickets2(2);
cinema.sellTickets2(4);
cinema.sellTickets1(2);
cinema.sellTickets1(1);
cinema.returnTickets2(2);
cinema.sellTickets1(3);
cinema.sellTickets2(2);
cinema.sellTickets1(2);
} }
Main.java
package com.dylan.thread.ch2.c02.core;

import com.dylan.thread.ch2.c02.task.Cinema;
import com.dylan.thread.ch2.c02.task.TicketOffice1;
import com.dylan.thread.ch2.c02.task.TicketOffice2; /**
* Core class of the example. Creates a cinema and two threads for
* the ticket office. Run the threads to analyze the results obtained
*
*/
public class Main { /**
* Main method of the example
* @param args
*/
public static void main(String[] args) {
// Creates a Cinema
Cinema cinema=new Cinema(); // Creates a TicketOffice1 and a Thread to run it
TicketOffice1 ticketOffice1=new TicketOffice1(cinema);
Thread thread1=new Thread(ticketOffice1,"TicketOffice1"); // Creates a TicketOffice2 and a Thread to run it
TicketOffice2 ticketOffice2=new TicketOffice2(cinema);
Thread thread2=new Thread(ticketOffice2,"TicketOffice2"); // Starts the threads
thread1.start();
thread2.start(); try {
// Waits for the finalization of the threads
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
} // Print the vacancies in the cinemas
System.out.printf("Room 1 Vacancies: %d\n",cinema.getVacanciesCinema1());
System.out.printf("Room 2 Vacancies: %d\n",cinema.getVacanciesCinema2());
} }

运行结果:

Room 1 Vacancies: 5
Room 2 Vacancies: 6

Java并发编程实例--14.在一个同步类中安排独立属性的更多相关文章

  1. Java并发编程:线程的同步

    Java并发编程:线程的同步 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} J ...

  2. Java并发编程的4个同步辅助类

    Java并发编程的4个同步辅助类(CountDownLatch.CyclicBarrier.Semphore.Phaser) @https://www.cnblogs.com/lizhangyong/ ...

  3. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semphore、Phaser)

    我在<jdk1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

  4. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semaphore、Phaser)

    我在<JDK1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

  5. Java并发编程(三)Thread类的使用

    一.线程的状态 线程从创建到最终的消亡,要经历若干个状态.一般来说,线程包括以下这几个状态:创建(new).就绪(runnable).运行(running).阻塞(blocked).time wait ...

  6. Java并发编程(六)-- 同步块

    上一节已经讲到,使用Synchronzied代码块可以解决共享对象的竞争问题,其实还有其他的方法也可以避免资源竞争问题,我统称他们为Java同步块.Java 同步块(synchronized bloc ...

  7. Java并发编程实例(synchronized)

    此处用一个小程序来说明一下,逻辑是一个计数器(int i):主要的逻辑功能是,如果同步监视了资源i,则不输出i的值,但如果没有添加关键字synchronized,因为是两个线程并发执行,所以会输出i的 ...

  8. Java并发编程(八)同步容器

    为了方便编写出线程安全的程序,Java里面提供了一些线程安全类和并发工具,比如:同步容器.并发容器.阻塞队列.Synchronizer(比如CountDownLatch) 一.为什么会出现同步容器? ...

  9. Java并发编程(二)同步

    在多线程的应用中,两个或者两个以上的线程需要共享对同一个数据的存取.如果两个线程存取相同的对象,并且每一个线程都调用了修改该对象的方法,这种情况通常成为竞争条件.  竞争条件最容易理解的例子就是:比如 ...

  10. [转]JAVA并发编程学习笔记之Unsafe类

    1.通过Unsafe类可以分配内存,可以释放内存:类中提供的3个本地方法allocateMemory.reallocateMemory.freeMemory分别用于分配内存,扩充内存和释放内存,与C语 ...

随机推荐

  1. [转帖]AF_UNIX 本地通信

    文章目录 一.AF_UNIX 本地通信 1. Linux进程通信机制 2. socket本地域套接字AF_UNIX 3. demo示例 二.AF_INET域与AF_UNIX域socket通信原理对比 ...

  2. [转帖]CertUtil: -hashfile 失败: 0xd00000bb (-805306181)

    https://www.cnblogs.com/heenhui2016/p/de.html 使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile ...

  3. CentOS7最小安装注意事项

    CentOS7最小安装注意事项 背景与说明 公司很多项目现在都是最小化安装的了. 自己之前为了方便都是使用GUI安装,很多地方可能都有遗漏 这里进行一下简单的安装总结, 避免以后新安装有缺失的项目 安 ...

  4. Mysql localhost 无法登录 root用户的处理过程

    问题说明: 前段时间同事修改密码, 但是发现修改了密码之后,外面可以连接root用户, 但是本地无法连接了. 怀疑是密码修改存在问题,需要重新进行处理 这里进行简单的描述. 问题现象 使用 mysql ...

  5. 浅析RobotFramework工具的使用 | 京东物流技术团队

    1 简介 最近几年越来越多的公司都开始进行自动化测试的设计和布局了,自动化,顾名思义就是把以人为驱动的测试行为转化为机器执行的一种过程,并经常用于回归测试中,市面上也存在很多开源的自动化测试的工具和理 ...

  6. 通过Environment获取属性文件的值,竟然会调用到JNDI服务!!!

    一.背景介绍 某应用在压测过程机器cpu使用率超过80%,通过在线诊断工具进行CPU采样生成的火焰图,看到程序中频繁调用environment.getProperty()获取属性值,而其内部调用了Jn ...

  7. [置顶] Gin--Begoo框架快速指南

    Gin篇 第一篇: Gin-官方文档 第二篇: Gin-安装,修改启动端口,get/post 请求参数,模型绑定shouldbind,自定义验证器/表单验证 第三篇: Gin-上传文件/返回前端.中间 ...

  8. 年末将至,Java 开发者必须了解的 16 个Java 顶级开源项目!

    年末将至,值得你关注的16个Java 开源项目! 本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb/JavaGuide ([Java学习+面试 ...

  9. Mygin中间件优化及日志中间件

    本篇是mygin的第七篇,参照gin框架,感兴趣的可以从 Mygin第一篇 开始看,Mygin从零开始完全手写,在实现的同时,带你一窥gin框架的核心原理实现. 目的 中间件Middleware优化 ...

  10. ABC311_g One More Grid Task 题解

    题目链接:Atcoder 或者 洛谷 对于解决二维区间内的最值类型问题,我们常常有一类特别好用的方法,就是悬线法,它可以看做是单调栈的子集,但更加好理解和书写. 对于悬线法,我们有一个常见的模型,找出 ...