为什么要使用同步锁?

因为当使用多线程同时访问一个变量或对象时,如果这些线程中即有读又有写操作时,会造成导致变量或对象的状态出现混乱。例如:一个银行账户被A/B两个线程同时操作,A线程、B线程同时开始操作:A线程存款100,B线程取款100,此时就会出现账户存款100,然后查询存储结果为0,B取款失败,但是查询余额为100。

而同步锁出现的目的就是为了解决多线程安全问题。

上边的举例对应的代码如下:

银行类:

/**
* 银行类
* */
public class Bank {
private int money = 0; /**
* 存款
*/
public void deposit(int money) {
this.money += money;
System.out.println(Thread.currentThread().getName() + ":存款" + money + ",賬戶餘額:" + this.money);
} /**
* 取款
*/
public void withdrawal(int money) {
if (this.money - money < 0) {
System.out.println(Thread.currentThread().getName() + ":余额不足");
return;
}
this.money -= money;
System.out.println(Thread.currentThread().getName() + ":取款" + money + ",賬戶餘額:" + this.money);
} /**
* 查詢賬戶餘額
*/
public void look() {
System.out.println(Thread.currentThread().getName() + ":查詢賬戶餘額為:" + this.money);
}
}

取款、存款A、B线程模拟:

public class LockTest {
public static void main(String[] args) {
final Bank bank = new Bank(); Thread threadA = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bank.deposit(100);
bank.look(); System.out.println("-----------------------");
}
}
}, "thread-a"); Thread threadB = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println("-----------------------");
bank.withdrawal(100);
bank.look(); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "thread-b"); threadB.start();
threadA.start();
}
}

此时打印的结果为:

-----------------------
thread-b:余额不足
thread-b:查詢賬戶餘額為:0
-----------------------
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:0
thread-b:取款100,賬戶餘額:0
thread-b:查詢賬戶餘額為:0
-----------------------

同步代码块:

注意:同步是一种高开销的操作,因此应该尽量减少同步的内容。通常没有必要同步整个方法,使用synchronized代码块同步关键代码即可。

/**
* 银行类
* */
class Bank {
private int money = 0; /**
* 存款
*/
public void deposit(int money) {
synchronized (this) {
this.money += money;
System.out.println(Thread.currentThread().getName() + ":存款" + money + ",賬戶餘額:" + this.money);
}
} /**
* 取款
*/
public void withdrawal(int money) {
synchronized (this) {
if (this.money - money < 0) {
System.out.println(Thread.currentThread().getName() + ":余额不足");
return;
}
this.money -= money;
System.out.println(Thread.currentThread().getName() + ":取款" + money + ",賬戶餘額:" + this.money);
}
} /**
* 查詢賬戶餘額
*/
public void look() {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + ":查詢賬戶餘額為:" + this.money);
}
}
}

测试:

-----------------------
thread-b:余额不足
thread-b:查詢賬戶餘額為:0
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100
-----------------------
-----------------------
thread-b:取款100,賬戶餘額:0
thread-b:查詢賬戶餘額為:0
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100
-----------------------
-----------------------
thread-b:取款100,賬戶餘額:0
thread-b:查詢賬戶餘額為:0
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100

同步方法:

同步方法就是使用synchronized关键字修饰某个方法,这个方法就是同步方法。这个同步方法(非static方法)无须显式指定同步监视器,同步方法的同步监视器是this,也就是调用该方法的对象。通过同步方法可以非常方便的实现线程安全的类,线程安全的类有如下特征:

每个线程调用该对象的任意synchronized方法之后,都能得到正确的结果;
每个线程调用该对象的任意synchronized方法之后,该对象状态依然能保持合理状态。
/**
* 银行类
*/
class Bank {
private int money = 0; /**
* 存款
*/
public synchronized void deposit(int money) {
this.money += money;
System.out.println(Thread.currentThread().getName() + ":存款" + money + ",賬戶餘額:" + this.money);
} /**
* 取款
*/
public synchronized void withdrawal(int money) {
if (this.money - money < 0) {
System.out.println(Thread.currentThread().getName() + ":余额不足");
return;
}
this.money -= money;
System.out.println(Thread.currentThread().getName() + ":取款" + money + ",賬戶餘額:" + this.money);
} /**
* 查詢賬戶餘額
*/
public synchronized void look() {
System.out.println(Thread.currentThread().getName() + ":查詢賬戶餘額為:" + this.money);
}
}

测试:

-----------------------
thread-b:余额不足
thread-b:查詢賬戶餘額為:0
-----------------------
thread-a:存款100,賬戶餘額:100
thread-b:取款100,賬戶餘額:0
thread-b:查詢賬戶餘額為:0
thread-a:查詢賬戶餘額為:0
-----------------------
-----------------------
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100
-----------------------

重入锁Lock

JavaSE5.0中新增了一个java.util.concurrent包来支持同步。ReentrantLock类是可重入、互斥、实现了Lock接口的锁, 它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力。
ReenreantLock类的常用方法有:

Lock lock=new ReentrantLock() ;// 创建一个ReentrantLock实例
lock.lock(); // 获得锁
lock.unlock();// 释放锁

注意:ReentrantLock()还有一个可以创建公平锁的构造方法,但由于能大幅度降低程序运行效率,不推荐使用。

/**
* 银行类
*/
class Bank {
private int money = 0;
private Lock lock=new ReentrantLock();
/**
* 存款
*/
public void deposit(int money) {
lock.lock();
try{
this.money += money;
System.out.println(Thread.currentThread().getName() + ":存款" + money + ",賬戶餘額:" + this.money);
}finally{
lock.unlock();
}
} /**
* 取款
*/
public void withdrawal(int money) {
lock.lock();
try{
if (this.money - money < 0) {
System.out.println(Thread.currentThread().getName() + ":余额不足");
return;
}
this.money -= money;
System.out.println(Thread.currentThread().getName() + ":取款" + money + ",賬戶餘額:" + this.money);
}finally{
lock.unlock();
}
} /**
* 查詢賬戶餘額
*/
public void look() {
lock.lock();
try{
System.out.println(Thread.currentThread().getName() + ":查詢賬戶餘額為:" + this.money);
}finally{
lock.unlock();
}
}
}

测试:

-----------------------
thread-b:余额不足
thread-b:查詢賬戶餘額為:0
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100
-----------------------
-----------------------
thread-b:取款100,賬戶餘額:0
thread-b:查詢賬戶餘額為:0
thread-a:存款100,賬戶餘額:100
thread-a:查詢賬戶餘額為:100
-----------------------

Java-JUC(七):同步锁的几种方式的更多相关文章

  1. Java反射获取class对象的三种方式,反射创建对象的两种方式

    Java反射获取class对象的三种方式,反射创建对象的两种方式 1.获取Class对象 在 Java API 中,提供了获取 Class 类对象的三种方法: 第一种,使用 Class.forName ...

  2. Java - "JUC" ReentrantLock获取锁

    [Java并发编程实战]-----“J.U.C”:ReentrantLock之一简介 ReentrantLock介绍 ReentrantLock是一个可重入的互斥锁,又被称为“独占锁”. 顾名思义,R ...

  3. JUC——线程同步锁(Condition精准控制)

    在进行锁处理的时候还有一个接口:Condition,这个接口可以由用户来自己进行锁的对象创建. Condition的作用是对锁进行更精确的控制. Condition的await()方法相当于Objec ...

  4. JAVA并发,同步锁性能测试

    测试主要从运行时间差来体现,数据量越大,时间差越明显,例子如下: package com.xt.thinks21_2; /** * 同步锁性能测试 * * @author Administrator ...

  5. Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition

    在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...

  6. Java中 实现多线程成的三种方式(继承,实现,匿名内部类)

    ---------------------------------------------------------------------------------------------------- ...

  7. Java使用基本字节流OutputStream的四种方式对于数据复制(文本,音视频,图像等数据)

    //package 字符缓冲流bufferreaderDemo; import java.io.BufferedOutputStream; import java.io.FileInputStream ...

  8. Java字符流读写数据的两种方式

    第一种方式:逐个字符进行读写操作(代码注释以及详细内容空闲补充) package IODemo; import java.io.FileReader; import java.io.FileWrite ...

  9. java 学习笔记 读取配置文件的三种方式

    package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...

随机推荐

  1. FireDAC 下的 Sqlite [10] - 使用 R-Tree 搜索

    R-Tree 主要用于三维空间的搜索, 据说这种搜索算法非常之快, 哪怕百万条记录也是眨眼间的事! SQLite 支持 1-5 维, FireDAC 也提供了 TFDSQLiteRTree 控件以方便 ...

  2. LPC43xx SGPIO Slice 示意图

    SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...

  3. [Ubuntu] 编译安装 PHP 依赖库

    编译环境 sudo apt-get -y install build-essential xml sudo apt-get -y install libxml2-dev pcre sudo apt-g ...

  4. Introduction to the Optimizer --cbo

    http://docs.oracle.com/cd/B10500_01/server.920/a96533/optimops.htm

  5. TF400511: Your team has not defined any iterations to use as sprints

    tfs里面的冲刺对于开发团队来说, 是非常重要的一个功能,是团队开发进度的晴雨表: 但是如果从此死活出不来,怎么办呢? TF400511:您的团队尚未定义任何要用作冲刺 (sprint) 的迭代 TF ...

  6. The .NET weak event pattern in C#

    Introduction As you may know event handlers are a common source of memory leaks caused by the persis ...

  7. Swift:playground

    在介绍Playground之前,我先罗列一些本人认为有点重要然而零碎的知识点. 1. Swift语法.每句话之后不用加分号.但也能够加分号.但假设写在一行的话.必须加分号. 2. Swift严格要求变 ...

  8. Struts2学习笔记——Struts2与Spring整合

      Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spring创建的 ...

  9. jQuery - 同时添加click和dblclick事件

    添加事件的代码比较简单,有两种方法: $("abc").bind({"click":fn,"dblclick":fn}); $(" ...

  10. IT狂人第一至四季/全集The IT Crowd迅雷下载

    本季第一至四季 The IT Crowd (2006)看点:<IT狂人>史上最囧,最雷,最脑残,最出乎意料,最不按常理出牌的IT “精英们”登上银屏了.让超擅长收发邮件.单击和双击鼠标的I ...