文档章节

并发编程实战 2.1. 使用synchronized实现同步

火犀牛
 火犀牛
发布于 2016/05/12 17:22
字数 1343
阅读 4
收藏 0

如果一个对象的方法已用synchronized关键字声明,那么只有一个线程被允许访问它。如果其他线程想要访问这个对象的其他方法,它将被挂起,直到第一个线程执行完正在执行的方法。

静态方法则有不同的行为。用synchronized声明的静态方法,同时只能被一个线程访问,但是其他线程可以访问这个对象的非静态方法。会造成数据不一致的错误。

范例:两个线程:银行存钱,公司取钱;一个实体类:账户,并带有存钱与取钱的方法。

账户:

package com.rr.concurrent.chapter2.recipe1.test;

import java.util.concurrent.TimeUnit;

/**
 * Created by isaac_gu on 2016/5/12.
 */
public class Account {
    private int balance;

    public Account(int balance) {
        this.balance = balance;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public synchronized void saveMoney(int amount) {
        int temp = balance;
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        temp += amount;
        balance = temp;
        System.out.println("saveMoney:" + balance);
    }

    public synchronized void drawMoney(int amount) {
        int temp = balance;
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        temp -= amount;
        balance = temp;
        System.out.println("drawMoney:" + balance);
    }
}

银行:

package com.rr.concurrent.chapter2.recipe1.test;

/**
 * Created by isaac_gu on 2016/5/12.
 * 银行负责存钱
 */
public class Bank implements Runnable {

    private Account account;

    public Bank(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0, len = 100; i < len; i++) {
            account.saveMoney(1000);
        }
    }
}

公司:

package com.rr.concurrent.chapter2.recipe1.test;

/**
 * Created by isaac_gu on 2016/5/12.
 * 公司负责取钱
 */
public class Company implements Runnable {
    private Account account;

    public Company(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0, len = 100; i < len; i++) {
            account.drawMoney(1000);
        }
    }
}

测试:

package com.rr.concurrent.chapter2.recipe1.test;

/**
 * Created by isaac_gu on 2016/5/12.
 * 范例:两个线程:银行存钱,公司取钱;一个实体类:账户,并带有存钱与取钱的方法。
 */
public class Test {
    public static void main(String[] args) {
        Account account = new Account(100000);

        Thread bankThread = new Thread(new Bank(account));
        Thread companyThread = new Thread(new Company(account));

        System.out.println("START - balance: " + account.getBalance());
        bankThread.start();
        companyThread.start();

        try {
            bankThread.join();
            companyThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("END - balance: "+ account.getBalance());

    }
}

同步的结果:

START - balance: 100000
saveMoney:101000
saveMoney:102000
saveMoney:103000
saveMoney:104000
saveMoney:105000
saveMoney:106000
saveMoney:107000
saveMoney:108000
drawMoney:107000
drawMoney:106000
drawMoney:105000
drawMoney:104000
drawMoney:103000
drawMoney:102000
drawMoney:101000
drawMoney:100000
drawMoney:99000
drawMoney:98000
drawMoney:97000
drawMoney:96000
drawMoney:95000
drawMoney:94000
drawMoney:93000
drawMoney:92000
drawMoney:91000
drawMoney:90000
drawMoney:89000
drawMoney:88000
drawMoney:87000
drawMoney:86000
drawMoney:85000
drawMoney:84000
drawMoney:83000
drawMoney:82000
drawMoney:81000
drawMoney:80000
drawMoney:79000
drawMoney:78000
drawMoney:77000
drawMoney:76000
drawMoney:75000
drawMoney:74000
drawMoney:73000
drawMoney:72000
drawMoney:71000
drawMoney:70000
drawMoney:69000
drawMoney:68000
drawMoney:67000
drawMoney:66000
drawMoney:65000
drawMoney:64000
drawMoney:63000
drawMoney:62000
drawMoney:61000
drawMoney:60000
drawMoney:59000
drawMoney:58000
drawMoney:57000
drawMoney:56000
drawMoney:55000
drawMoney:54000
drawMoney:53000
drawMoney:52000
drawMoney:51000
drawMoney:50000
drawMoney:49000
drawMoney:48000
drawMoney:47000
drawMoney:46000
drawMoney:45000
drawMoney:44000
drawMoney:43000
drawMoney:42000
drawMoney:41000
drawMoney:40000
drawMoney:39000
drawMoney:38000
drawMoney:37000
drawMoney:36000
drawMoney:35000
drawMoney:34000
drawMoney:33000
drawMoney:32000
drawMoney:31000
drawMoney:30000
drawMoney:29000
drawMoney:28000
drawMoney:27000
drawMoney:26000
drawMoney:25000
drawMoney:24000
drawMoney:23000
drawMoney:22000
drawMoney:21000
drawMoney:20000
drawMoney:19000
drawMoney:18000
drawMoney:17000
drawMoney:16000
drawMoney:15000
drawMoney:14000
drawMoney:13000
drawMoney:12000
drawMoney:11000
drawMoney:10000
drawMoney:9000
drawMoney:8000
saveMoney:9000
saveMoney:10000
saveMoney:11000
saveMoney:12000
saveMoney:13000
saveMoney:14000
saveMoney:15000
saveMoney:16000
saveMoney:17000
saveMoney:18000
saveMoney:19000
saveMoney:20000
saveMoney:21000
saveMoney:22000
saveMoney:23000
saveMoney:24000
saveMoney:25000
saveMoney:26000
saveMoney:27000
saveMoney:28000
saveMoney:29000
saveMoney:30000
saveMoney:31000
saveMoney:32000
saveMoney:33000
saveMoney:34000
saveMoney:35000
saveMoney:36000
saveMoney:37000
saveMoney:38000
saveMoney:39000
saveMoney:40000
saveMoney:41000
saveMoney:42000
saveMoney:43000
saveMoney:44000
saveMoney:45000
saveMoney:46000
saveMoney:47000
saveMoney:48000
saveMoney:49000
saveMoney:50000
saveMoney:51000
saveMoney:52000
saveMoney:53000
saveMoney:54000
saveMoney:55000
saveMoney:56000
saveMoney:57000
saveMoney:58000
saveMoney:59000
saveMoney:60000
saveMoney:61000
saveMoney:62000
saveMoney:63000
saveMoney:64000
saveMoney:65000
saveMoney:66000
saveMoney:67000
saveMoney:68000
saveMoney:69000
saveMoney:70000
saveMoney:71000
saveMoney:72000
saveMoney:73000
saveMoney:74000
saveMoney:75000
saveMoney:76000
saveMoney:77000
saveMoney:78000
saveMoney:79000
saveMoney:80000
saveMoney:81000
saveMoney:82000
saveMoney:83000
saveMoney:84000
saveMoney:85000
saveMoney:86000
saveMoney:87000
saveMoney:88000
saveMoney:89000
saveMoney:90000
saveMoney:91000
saveMoney:92000
saveMoney:93000
saveMoney:94000
saveMoney:95000
saveMoney:96000
saveMoney:97000
saveMoney:98000
saveMoney:99000
saveMoney:100000
END - balance: 100000

去掉synchronized的结果

START - balance: 100000
saveMoney:101000
drawMoney:99000
saveMoney:102000
drawMoney:98000
saveMoney:103000
drawMoney:97000
saveMoney:104000
drawMoney:96000
saveMoney:105000
drawMoney:95000
saveMoney:106000
drawMoney:94000
saveMoney:107000
drawMoney:93000
saveMoney:108000
drawMoney:92000
saveMoney:109000
drawMoney:91000
saveMoney:110000
drawMoney:90000
saveMoney:111000
drawMoney:89000
drawMoney:88000
saveMoney:112000
saveMoney:113000
drawMoney:87000
saveMoney:114000
drawMoney:86000
drawMoney:85000
saveMoney:85000
saveMoney:86000
drawMoney:84000
drawMoney:83000
saveMoney:83000
saveMoney:84000
drawMoney:84000
drawMoney:83000
saveMoney:85000
saveMoney:86000
drawMoney:84000
drawMoney:85000
saveMoney:85000
saveMoney:86000
drawMoney:84000
saveMoney:83000
drawMoney:83000
drawMoney:82000
saveMoney:84000
saveMoney:85000
drawMoney:81000
saveMoney:86000
drawMoney:80000
drawMoney:79000
saveMoney:79000
drawMoney:78000
saveMoney:80000
drawMoney:77000
saveMoney:81000
drawMoney:80000
saveMoney:82000
saveMoney:83000
drawMoney:81000
drawMoney:80000
saveMoney:84000
drawMoney:79000
saveMoney:85000
drawMoney:78000
saveMoney:86000
drawMoney:77000
saveMoney:87000
drawMoney:76000
saveMoney:88000
drawMoney:75000
saveMoney:89000
drawMoney:74000
saveMoney:90000
drawMoney:73000
saveMoney:91000
drawMoney:72000
saveMoney:92000
drawMoney:71000
saveMoney:93000
drawMoney:70000
saveMoney:94000
drawMoney:69000
saveMoney:95000
drawMoney:68000
saveMoney:96000
drawMoney:67000
saveMoney:97000
drawMoney:66000
saveMoney:98000
drawMoney:65000
saveMoney:99000
drawMoney:64000
saveMoney:100000
drawMoney:63000
saveMoney:101000
drawMoney:62000
saveMoney:102000
drawMoney:61000
saveMoney:103000
drawMoney:60000
saveMoney:104000
drawMoney:59000
saveMoney:105000
drawMoney:58000
saveMoney:106000
drawMoney:57000
saveMoney:107000
drawMoney:56000
saveMoney:108000
drawMoney:55000
saveMoney:109000
drawMoney:54000
saveMoney:110000
drawMoney:53000
saveMoney:111000
drawMoney:52000
saveMoney:112000
saveMoney:113000
drawMoney:51000
drawMoney:50000
saveMoney:114000
drawMoney:49000
saveMoney:115000
saveMoney:116000
drawMoney:48000
drawMoney:47000
saveMoney:117000
drawMoney:116000
saveMoney:116000
saveMoney:117000
drawMoney:115000
saveMoney:118000
drawMoney:114000
saveMoney:115000
drawMoney:113000
saveMoney:114000
drawMoney:112000
saveMoney:113000
drawMoney:111000
drawMoney:110000
saveMoney:112000
saveMoney:111000
drawMoney:111000
saveMoney:112000
drawMoney:112000
saveMoney:113000
drawMoney:111000
saveMoney:114000
drawMoney:110000
saveMoney:115000
drawMoney:109000
drawMoney:108000
saveMoney:116000
saveMoney:117000
drawMoney:107000
saveMoney:118000
drawMoney:106000
drawMoney:105000
saveMoney:119000
saveMoney:120000
drawMoney:104000
drawMoney:103000
saveMoney:121000
drawMoney:102000
saveMoney:122000
drawMoney:101000
saveMoney:123000
drawMoney:100000
saveMoney:124000
drawMoney:99000
saveMoney:125000
saveMoney:126000
drawMoney:98000
saveMoney:127000
drawMoney:97000
saveMoney:128000
drawMoney:96000
saveMoney:129000
drawMoney:95000
saveMoney:130000
drawMoney:94000
saveMoney:131000
drawMoney:93000
saveMoney:132000
drawMoney:92000
drawMoney:91000
saveMoney:133000
drawMoney:90000
saveMoney:134000
saveMoney:135000
drawMoney:133000
drawMoney:132000
saveMoney:136000
saveMoney:137000
drawMoney:131000
drawMoney:130000
saveMoney:132000
END - balance: 132000

 

© 著作权归作者所有

共有 人打赏支持
火犀牛
粉丝 0
博文 27
码字总数 12180
作品 0
海淀
程序员
私信 提问
synchronized使用及实现原理

1.Synchronized的基本使用 Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法。Synchronized的作用主要有三个:(1)确保线程互斥的访问同步代码(2)保证共享变量...

梨加橙
2018/06/28
0
0
Java可重入锁学习笔记

目录 什么是可重入锁 为什么要可重入 如何实现可重入锁 有不可重入锁吗 demo代码展示 参考文章 1 . 什么是可重入锁 锁的概念就不用多解释了,当某个线程A已经持有了一个锁,当线程B尝试进入被这...

squanchao
2016/07/12
36
1
15《Java核心技术》之synchronized和ReentrantLock有什么区别呢?

一、提出问题 从今天开始,我们将进入 Java 并发学习阶段。软件并发已经成为现代软件开发的基础能力,而 Java 精心设计的高效并发机制,正是构建大规模应用的基础之一,所以考察并发基本功也...

飞鱼说编程
2018/11/05
0
0
Java 重入锁 ReentrantLock 原理分析

1.简介 可重入锁自 JDK 1.5 被引入,功能上与关键字类似。所谓的可重入是指,线程可对同一把锁进行重复加锁,而不会被阻塞住,这样可避免死锁的产生。ReentrantLock 的主要功能和 synchroniz...

Java-老刘
2018/05/08
0
0
学习笔记6:synchronized关键字-->线程同步

为什么要使用关键字synchronized? java程序中可以存在多个线程,但是当多个线程操作同一资源时候,可能会导致数据不一致。 区分synchronized方法与synchronized块: synchronized方法是一种粗...

牧羊人Berg
2015/11/07
0
0

没有更多内容

加载失败,请刷新页面

加载更多

centos7重置密码、单用户模式、救援模式、ls命令、chmod命令

在工作当中如果我们错误的配置了文件使服务器不能正常启动或者忘记密码不能登录系统,如何解决这些问题呢?重装系统是可以实现的,但是往往不能轻易重装系统的,下面用忘记密码作为例子讲解如...

李超小牛子
今天
3
0
Python如何开发桌面应用程序?Python基础教程,第十三讲,图形界面

当使用桌面应用程序的时候,有没有那么一瞬间,想学习一下桌面应用程序开发?行业内专业的桌面应用程序开发一般是C++,C#来做,Java开发的也有,但是比较少。本节课会介绍Python的GUI(图形用...

程序员补给栈
今天
6
0
kafka在的使用

一、基本概念 介绍 Kafka是一个分布式的、可分区的、可复制的消息系统。它提供了普通消息系统的功能,但具有自己独特的设计。 这个独特的设计是什么样的呢? 首先让我们看几个基本的消息系统...

狼王黄师傅
今天
3
0
Android JNI总结

0x01 JNI介绍 JNI是Java Native Interface的缩写,JNI不是Android专有的东西,它是从Java继承而来,但是在Android中,JNI的作用和重要性大大增强。 JNI在Android中起着连接Java和C/C++层的作...

天王盖地虎626
昨天
3
0
大数据教程(11.8)Hive1.2.2简介&初体验

上一篇文章分析了Hive1.2.2的安装,本节博主将分享Hive的体验&Hive服务端和客户端的使用方法。 一、Hive与hadoop直接的关系 Hive利用HDFS存储数据,利用MapReduce查询数据。 二、Hive与传统数...

em_aaron
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部