赞
踩
首先,我们得知道在java中存在三种变量:
然后,我们得明白,合适会发生高并发不安全
具体不安全案例请参考 如下这篇文章:java线程安全问题详解_我想月薪过万的博客-CSDN博客https://blog.csdn.net/qq_41885673/article/details/121431714
在上面这篇文章银行取钱案例中,我们解决线程安全问题的方法是加了一个 synchronized 关键字。下面我们就详细介绍一下 synchronized 的三种写法,分别解决什么问题!!!
- package ThreadSafa;
-
- public class Test {
- public static void main(String[] args) {
- TestAccount ta1 = new TestAccount();
- ta1.setNum(10);
-
- //共用一个账户对象
- TestThread t1 = new TestThread(ta1);
- TestThread t2 = new TestThread(ta1);
- t1.start();
- t2.start();
- }
- }
-
- class TestThread extends Thread {
-
- private TestAccount mAccount;
-
- public TestThread(TestAccount mAccount) {
- this.mAccount = mAccount;
- }
-
- @Override
- public void run() {
- mAccount.updateNum(1);
- }
- }
-
- class TestAccount {
- private double num;
-
- public double getNum() {
- return num;
- }
-
- public void setNum(double num) {
- this.num = num;
- }
-
- public void updateNum(int n) {
- synchronized (this) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- setNum(getNum() - n);
- }
- System.out.println(getNum());
- }
- }
运行结果
- package ThreadSafa;
-
- public class Test {
- public static void main(String[] args) {
- TestAccount ta1 = new TestAccount();
- ta1.setNum(10);
-
- TestThread t1 = new TestThread(ta1);
- TestThread t2 = new TestThread(ta1);
- t1.start();
- t2.start();
- }
- }
-
- class TestThread extends Thread {
-
- private TestAccount mAccount;
-
- public TestThread(TestAccount mAccount) {
- this.mAccount = mAccount;
- }
-
- @Override
- public void run() {
- mAccount.updateNum(1);
- }
- }
-
- class TestAccount {
- private double num;
-
- public double getNum() {
- return num;
- }
-
- public void setNum(double num) {
- this.num = num;
- }
-
- public synchronized void updateNum(int n) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- setNum(getNum() - n);
- System.out.println(getNum());
- }
- }
运行结果
可以看到 ,前面这两种写法其实是等价的,什么意思呢?就是当你用 synchronized 修饰共享对象 this 的时候 你就可以吧 synchronized 提到方法前面,但是我们一般不会这么干,因为扩大 synchronized 修饰的代码范围会使代码运行效率降低。
同时,前面两种方法都是为了解决 实例变量 线程安全问题而诞生的,对于静态变量我们怎么处理呢?请看写法三:
- package ThreadSafa;
-
- public class Test {
- public static void main(String[] args) {
- TestAccount ta1 = new TestAccount();
- TestAccount ta2 = new TestAccount();
-
- TestThread t1 = new TestThread(ta1);
- TestThread t2 = new TestThread(ta2);
- t1.start();
- t2.start();
- }
- }
-
- class TestThread extends Thread {
-
- private TestAccount mAccount;
-
- public TestThread(TestAccount mAccount) {
- this.mAccount = mAccount;
- }
-
- @Override
- public void run() {
- mAccount.updateCount(1);
- }
- }
-
- class TestAccount {
- private double num;
- public static double count = 10;
-
- public double getNum() {
- return num;
- }
-
- public void setNum(double num) {
- this.num = num;
- }
-
- public synchronized void updateNum(int n) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- setNum(getNum() - n);
- System.out.println(getNum());
- }
-
- public synchronized static void updateCount(int n) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- count -= n;
- System.out.println(count);
- }
- }
运行结果展示
可以看到,在静态方法上加 synchronized 之后,他锁的是这个类,尽管两个账户对象不一样,但是,加了 synchronized 会保证他们排队执行,也就是保证了线程安全。
局部变量 =》 存在于栈中 =》 线程之间不能共享 =》 所以数据永远是安全的
实例变量 =》 存在于堆中 =》 线程之间能共享 =》 采用写法一和写法二保证线程安全
静态变量 =》 存在于方法区 =》 线程之间能共享 =》 采用方写法三保证线程安全
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。