并发编程实战 2.1. 使用synchronized实现同步
并发编程实战 2.1. 使用synchronized实现同步
火犀牛 发表于2年前
并发编程实战 2.1. 使用synchronized实现同步
  • 发表于 2年前
  • 阅读 4
  • 收藏 0
  • 点赞 2
  • 评论 0

移动开发云端新模式探索实践 >>>   

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

如果一个对象的方法已用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
×
火犀牛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: