4.脏读。

  1. 对于对象的同步和异步的方法,在设计程序的时候一定要考虑问题的整体性,不然会出现数据不一致的错误,经典的就是脏读。
  2.   在对对象加锁的时候,要考虑业务的整体性,及为setValue/getVaule方法同时加锁synchronized关键字,保证业务的原子性,不然会出现错误。
  3. public class DirtyRead {
  4.  
  5.    private String username = "fdfdf";
  6.    private String password = "123456";
  7.   
  8.    public synchronized void setValue(String username, String password){
  9.       this.username = username;
  10.      
  11.       try {
  12.          Thread.sleep(2000);
  13.       } catch (InterruptedException e) {
  14.          e.printStackTrace();
  15.       }
  16.      
  17.       this.password = password;
  18.      
  19.       System.out.println("setValue最终结果:username = " + username + " , password = " + password);
  20.    }
  21.   
  22.    public  void getValue(){
  23.       System.out.println("getValue方法得到:username = " + this.username + " , password = " + this.password);
  24.    }
  25.   
  26.   
  27.    public static void main(String[] args) throws Exception{
  28.      
  29.       final DirtyRead dr = new DirtyRead();
  30.       Thread t1 = new Thread(new Runnable() {
  31.          @Override
  32.          public void run() {
  33.             dr.setValue("z3", "456");   
  34.          }
  35.       });
  36.       t1.start();
  37.       Thread.sleep(1000);
  38.      
  39.       dr.getValue();
  40.    }
  41.   
  42.   
  43.   
  44. }
  45. 输出结果:
  46. getValue方法得到:username = z3 , password = 123456
  47. setValue最终结果:username = z3 , password = 456
  48.  
  49.  
  50. public class DirtyRead {
  51.  
  52.    private String username = "fdfdf";
  53.    private String password = "123456";
  54.   
  55.    public synchronized void setValue(String username, String password){
  56.       this.username = username;
  57.      
  58.       try {
  59.          Thread.sleep(2000);
  60.       } catch (InterruptedException e) {
  61.          e.printStackTrace();
  62.       }
  63.      
  64.       this.password = password;
  65.      
  66.       System.out.println("setValue最终结果:username = " + username + " , password = " + password);
  67.    }
  68.    加上synchronized关键字
  69.    public synchronized void getValue(){
  70.       System.out.println("getValue方法得到:username = " + this.username + " , password = " + this.password);
  71.    }
  72.   
  73.   
  74.    public static void main(String[] args) throws Exception{
  75.      
  76.       final DirtyRead dr = new DirtyRead();
  77.       Thread t1 = new Thread(new Runnable() {
  78.          @Override
  79.          public void run() {
  80.             dr.setValue("z3", "456");   
  81.          }
  82.       });
  83.       t1.start();
  84.       Thread.sleep(1000);
  85.      
  86.       dr.getValue();
  87.    }
  88.   
  89.   
  90.   
  91. }
  92. 输出结果:
  93. setValue最终结果:username = z3 , password = 456
  94. getValue方法得到:username = z3 , password = 456
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值