• 1.7.7释放锁的不良后果


    使用stop释放锁将会导致数据出现不一致的结果。

    测试如下

     1 package com.cky.bean;
     2 
     3 /**
     4  * Created by edison on 2017/12/3.
     5  */
     6 public class SynchroizeObject {
     7     private String usrname="a";
     8     private String pwd ="aa";
     9 
    10     public String getUsrname() {
    11         return usrname;
    12     }
    13 
    14     public void setUsrname(String usrname) {
    15         this.usrname = usrname;
    16     }
    17 
    18     public String getPwd() {
    19         return pwd;
    20     }
    21 
    22     public void setPwd(String pwd) {
    23         this.pwd = pwd;
    24     }
    25 
    26     synchronized  public void printString(String usrname, String pwd) {
    27         try {
    28             this.usrname = usrname;
    29             Thread.sleep(100000);
    30             this.pwd = pwd;
    31         } catch (InterruptedException e) {
    32             e.printStackTrace();
    33         }
    34     }
    35 }
     1 package com.cky.thread;
     2 
     3 import com.cky.bean.SynchroizeObject;
     4 
     5 /**
     6  * Created by edison on 2017/12/3.
     7  */
     8 public class MyThread extends Thread{
     9     private SynchroizeObject so;
    10     public MyThread (SynchroizeObject so) {
    11         super();
    12         this.so = so;
    13     }
    14     @Override
    15     public void run() {
    16        so.printString("b","bb");
    17     }
    18 }
    package com.cky.test;
    
    import com.cky.bean.SynchroizeObject;
    import com.cky.thread.MyThread;
    
    /**
     * Created by edison on 2017/12/3.
     */
    public class Test {
        public static void main(String[] args) {
            try {
                SynchroizeObject so = new SynchroizeObject();
                MyThread myThread = new MyThread(so);
                myThread.start();
                Thread.sleep(2000);
                myThread.stop();
                System.out.println(so.getUsrname()+ "..."+ so.getPwd());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
    ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
    esources.jar;C:itsoftjdkjrelib
    t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
    b...aa
    
    Process finished with exit code 0

    综上所述,表明stop方法会导致数据不同步,不建议使用

  • 相关阅读:
    菜鸟小结
    计算几何题目整理(转)
    poj 3299 Humidex
    基于C的文件操作(转)
    poj 1328 Radar Installation
    poj 1321 棋盘问题(dfs)
    poj 3302 Subsequence
    C# 资产(Property) 与普通字段(field)变量的区别
    Jumping into Cloud, Be Sure You Know How to Get Out
    关于语言的想法。
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7954764.html
Copyright © 2020-2023  润新知