一、目的
有赞突击面试,毫无防备,这个问题好像被别的公司也问过,没答上来,所以总结一下。
二、为什么要线程同步
三、不同情况下的结果
package com.thread.test;
public class Bank {
private int count = 0; //银行账户
//存钱
public void addMoney( int money) {
count += money;
System.out.println(System.currentTimeMillis()+"存进:" +money);
}
//取钱
public void subMoney(int money) {
if(count-money<0){
System.out.println("余额不足");
return;
}
count -= money;
System.out.println(+System.currentTimeMillis()+"取出+"+money);
}
//查询
public void lookMoney() {
System.out.println("账户余额:" +count);
}
}
package com.thread.test;
public class SyncThreadTest {
public static void main(String[] args) {
final Bank bank = new Bank();
Thread tadd = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
bank.addMoney(100);
bank.lookMoney();
System.out.println("\n");
}
}
});
Thread tsub = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try{
Thread.sleep(1000);
}catch (Exception e) {
e.printStackTrace();
}
bank.subMoney(100);
bank.lookMoney();
System.out.println("\n");
}
}
});
tsub.start();
tadd.start();
}
}
两个进程:(1)存钱,(2)取钱。
1528689141841存进:100
账户余额:100
1528689141942存进:100
账户余额:200
1528689142042存进:100
账户余额:300
1528689142142存进:100
账户余额:400
package com.thread.test;
public class SynchronizedBank {
private int count = 0; //银行账户
//存钱
public synchronized void addMoney( int money) {
count += money;
System.out.println(System.currentTimeMillis()+"存进:" +money);
}
//取钱
public synchronized void subMoney(int money) {
if(count-money<0){
System.out.println("余额不足");
return;
}
count -= money;
System.out.println(+System.currentTimeMillis()+"取出+"+money);
}
//查询
public void lookMoney() {
System.out.println("账户余额:" +count);
}
}
结果:
1528697968158存进:100
账户余额:100
1528697968162取出+100
账户余额:0
1528697969159存进:100
账户余额:100
1528697969163取出+100
账户余额:0
package com.thread.test;
public class SynchronizedBank_block {
private int count = 0; //银行账户
//存钱
public void addMoney( int money) {
synchronized (this) {
count += money;
}
System.out.println(System.currentTimeMillis()+"存进:" +money);
}
//取钱
public synchronized void subMoney(int money) {
synchronized(this){
if(count-money<0){
System.out.println("余额不足");
return;
}
count -= money;
}
System.out.println(+System.currentTimeMillis()+"取出+"+money);
}
//查询
public void lookMoney() {
System.out.println("账户余额:" +count);
}
}
运行结果:
1528699402342取出+100
账户余额:0
1528699403346存进:100
账户余额:100
1528699403347取出+100
账户余额:0
1528699404347存进:100
账户余额:100
注:同步是一种高开销的操作,因此应该尽量减少同步的内容。通常没有必要同步整个方法,使用synchronized代码块同步关键代码即可。
package com.thread.test;
public class VolatileBank {
private volatile int count = 0; //银行账户
//存钱
public void addMoney( int money) {
count += money;
System.out.println(System.currentTimeMillis()+"存进:" +money);
}
//取钱
public void subMoney(int money) {
if(count-money<0){
System.out.println("余额不足");
return;
}
count -= money;
System.out.println(+System.currentTimeMillis()+"取出+"+money);
}
//查询
public void lookMoney() {
System.out.println("账户余额:" +count);
}
}
余额不足
账户余额:0
1528700014203存进:100
账户余额:100
1528700015198取出+100
账户余额:0
1528700015203存进:100
账户余额:100
1528700016198取出+100
账户余额:0
1528700016203存进:100
账户余额:100
注:ReentrantLock()还有一个可以创建公平锁的构造方法,但由于能大幅度降低程序运行效率,不推荐使用
1528700957237存进:100
账户余额:100
1528700958238取出+100
账户余额:0
1528700958238存进:100
账户余额:100
1528700959238存进:100
1528700959238取出+100
账户余额:100
账户余额:100
1528700960239存进:100
账户余额:200
1528700960240取出+100
账户余额:100
package com.thread.test;
public class ThreadLocalBank {
private static ThreadLocal count = new ThreadLocal(){
@Override
protected Integer initialValue() {
return 10000;
}
};
//存钱
public void addMoney( int money) {
count.set(count.get()+money);
System.out.println(System.currentTimeMillis()+"存进:" +money);
}
//取钱
public void subMoney(int money) {
if(count.get()-money<0){
System.out.println("余额不足");
return;
}
count.set(count.get()-money);;
System.out.println(+System.currentTimeMillis()+"取出: "+money);
}
//查询
public void lookMoney() {
System.out.println("账户余额:" +count.get());
}
}
结果:
1528703214170取出: 100
账户余额:9900
1528703214170存进:100
账户余额:10100
1528703215171取出: 100
账户余额:9800
1528703215172存进:100
账户余额:10200
1528703216171取出: 100
账户余额:9700
1528703216172存进:100
账户余额:10300
1528703217171取出: 100
账户余额:9600
1528703217172存进:100
账户余额:10400
1528703218171取出: 100
账户余额:9500
1528703218172存进:100
账户余额:10500
1528703219171取出: 100
账户余额:9400
1528703219173存进:100
账户余额:10600
1528703220171取出: 100
账户余额:9300
1528703220173存进:100
账户余额:10700
参考:https://blog.csdn.net/wenwen091100304/article/details/48318699