本文共 8943 字,大约阅读时间需要 29 分钟。
如果一个对象的方法已用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: 100000saveMoney:101000saveMoney:102000saveMoney:103000saveMoney:104000saveMoney:105000saveMoney:106000saveMoney:107000saveMoney:108000drawMoney:107000drawMoney:106000drawMoney:105000drawMoney:104000drawMoney:103000drawMoney:102000drawMoney:101000drawMoney:100000drawMoney:99000drawMoney:98000drawMoney:97000drawMoney:96000drawMoney:95000drawMoney:94000drawMoney:93000drawMoney:92000drawMoney:91000drawMoney:90000drawMoney:89000drawMoney:88000drawMoney:87000drawMoney:86000drawMoney:85000drawMoney:84000drawMoney:83000drawMoney:82000drawMoney:81000drawMoney:80000drawMoney:79000drawMoney:78000drawMoney:77000drawMoney:76000drawMoney:75000drawMoney:74000drawMoney:73000drawMoney:72000drawMoney:71000drawMoney:70000drawMoney:69000drawMoney:68000drawMoney:67000drawMoney:66000drawMoney:65000drawMoney:64000drawMoney:63000drawMoney:62000drawMoney:61000drawMoney:60000drawMoney:59000drawMoney:58000drawMoney:57000drawMoney:56000drawMoney:55000drawMoney:54000drawMoney:53000drawMoney:52000drawMoney:51000drawMoney:50000drawMoney:49000drawMoney:48000drawMoney:47000drawMoney:46000drawMoney:45000drawMoney:44000drawMoney:43000drawMoney:42000drawMoney:41000drawMoney:40000drawMoney:39000drawMoney:38000drawMoney:37000drawMoney:36000drawMoney:35000drawMoney:34000drawMoney:33000drawMoney:32000drawMoney:31000drawMoney:30000drawMoney:29000drawMoney:28000drawMoney:27000drawMoney:26000drawMoney:25000drawMoney:24000drawMoney:23000drawMoney:22000drawMoney:21000drawMoney:20000drawMoney:19000drawMoney:18000drawMoney:17000drawMoney:16000drawMoney:15000drawMoney:14000drawMoney:13000drawMoney:12000drawMoney:11000drawMoney:10000drawMoney:9000drawMoney:8000saveMoney:9000saveMoney:10000saveMoney:11000saveMoney:12000saveMoney:13000saveMoney:14000saveMoney:15000saveMoney:16000saveMoney:17000saveMoney:18000saveMoney:19000saveMoney:20000saveMoney:21000saveMoney:22000saveMoney:23000saveMoney:24000saveMoney:25000saveMoney:26000saveMoney:27000saveMoney:28000saveMoney:29000saveMoney:30000saveMoney:31000saveMoney:32000saveMoney:33000saveMoney:34000saveMoney:35000saveMoney:36000saveMoney:37000saveMoney:38000saveMoney:39000saveMoney:40000saveMoney:41000saveMoney:42000saveMoney:43000saveMoney:44000saveMoney:45000saveMoney:46000saveMoney:47000saveMoney:48000saveMoney:49000saveMoney:50000saveMoney:51000saveMoney:52000saveMoney:53000saveMoney:54000saveMoney:55000saveMoney:56000saveMoney:57000saveMoney:58000saveMoney:59000saveMoney:60000saveMoney:61000saveMoney:62000saveMoney:63000saveMoney:64000saveMoney:65000saveMoney:66000saveMoney:67000saveMoney:68000saveMoney:69000saveMoney:70000saveMoney:71000saveMoney:72000saveMoney:73000saveMoney:74000saveMoney:75000saveMoney:76000saveMoney:77000saveMoney:78000saveMoney:79000saveMoney:80000saveMoney:81000saveMoney:82000saveMoney:83000saveMoney:84000saveMoney:85000saveMoney:86000saveMoney:87000saveMoney:88000saveMoney:89000saveMoney:90000saveMoney:91000saveMoney:92000saveMoney:93000saveMoney:94000saveMoney:95000saveMoney:96000saveMoney:97000saveMoney:98000saveMoney:99000saveMoney:100000END - balance: 100000
去掉synchronized的结果
START - balance: 100000saveMoney:101000drawMoney:99000saveMoney:102000drawMoney:98000saveMoney:103000drawMoney:97000saveMoney:104000drawMoney:96000saveMoney:105000drawMoney:95000saveMoney:106000drawMoney:94000saveMoney:107000drawMoney:93000saveMoney:108000drawMoney:92000saveMoney:109000drawMoney:91000saveMoney:110000drawMoney:90000saveMoney:111000drawMoney:89000drawMoney:88000saveMoney:112000saveMoney:113000drawMoney:87000saveMoney:114000drawMoney:86000drawMoney:85000saveMoney:85000saveMoney:86000drawMoney:84000drawMoney:83000saveMoney:83000saveMoney:84000drawMoney:84000drawMoney:83000saveMoney:85000saveMoney:86000drawMoney:84000drawMoney:85000saveMoney:85000saveMoney:86000drawMoney:84000saveMoney:83000drawMoney:83000drawMoney:82000saveMoney:84000saveMoney:85000drawMoney:81000saveMoney:86000drawMoney:80000drawMoney:79000saveMoney:79000drawMoney:78000saveMoney:80000drawMoney:77000saveMoney:81000drawMoney:80000saveMoney:82000saveMoney:83000drawMoney:81000drawMoney:80000saveMoney:84000drawMoney:79000saveMoney:85000drawMoney:78000saveMoney:86000drawMoney:77000saveMoney:87000drawMoney:76000saveMoney:88000drawMoney:75000saveMoney:89000drawMoney:74000saveMoney:90000drawMoney:73000saveMoney:91000drawMoney:72000saveMoney:92000drawMoney:71000saveMoney:93000drawMoney:70000saveMoney:94000drawMoney:69000saveMoney:95000drawMoney:68000saveMoney:96000drawMoney:67000saveMoney:97000drawMoney:66000saveMoney:98000drawMoney:65000saveMoney:99000drawMoney:64000saveMoney:100000drawMoney:63000saveMoney:101000drawMoney:62000saveMoney:102000drawMoney:61000saveMoney:103000drawMoney:60000saveMoney:104000drawMoney:59000saveMoney:105000drawMoney:58000saveMoney:106000drawMoney:57000saveMoney:107000drawMoney:56000saveMoney:108000drawMoney:55000saveMoney:109000drawMoney:54000saveMoney:110000drawMoney:53000saveMoney:111000drawMoney:52000saveMoney:112000saveMoney:113000drawMoney:51000drawMoney:50000saveMoney:114000drawMoney:49000saveMoney:115000saveMoney:116000drawMoney:48000drawMoney:47000saveMoney:117000drawMoney:116000saveMoney:116000saveMoney:117000drawMoney:115000saveMoney:118000drawMoney:114000saveMoney:115000drawMoney:113000saveMoney:114000drawMoney:112000saveMoney:113000drawMoney:111000drawMoney:110000saveMoney:112000saveMoney:111000drawMoney:111000saveMoney:112000drawMoney:112000saveMoney:113000drawMoney:111000saveMoney:114000drawMoney:110000saveMoney:115000drawMoney:109000drawMoney:108000saveMoney:116000saveMoney:117000drawMoney:107000saveMoney:118000drawMoney:106000drawMoney:105000saveMoney:119000saveMoney:120000drawMoney:104000drawMoney:103000saveMoney:121000drawMoney:102000saveMoney:122000drawMoney:101000saveMoney:123000drawMoney:100000saveMoney:124000drawMoney:99000saveMoney:125000saveMoney:126000drawMoney:98000saveMoney:127000drawMoney:97000saveMoney:128000drawMoney:96000saveMoney:129000drawMoney:95000saveMoney:130000drawMoney:94000saveMoney:131000drawMoney:93000saveMoney:132000drawMoney:92000drawMoney:91000saveMoney:133000drawMoney:90000saveMoney:134000saveMoney:135000drawMoney:133000drawMoney:132000saveMoney:136000saveMoney:137000drawMoney:131000drawMoney:130000saveMoney:132000END - balance: 132000
转载地址:http://ojzua.baihongyu.com/