码迷,mamicode.com
首页 > 编程语言 > 详细

004JAVA多线程脏读

时间:2019-03-27 18:19:00      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:脏读   syn   rgs   oss   const   run   cto   ack   inter   

package com.skcc.mthread;

public class A_003DirtRead {

private String username="zjy";
private String password="123456";

public A_003DirtRead() {
    // TODO Auto-generated constructor stub
}

public synchronized void setValue(String username,String password) {
    this.username = username;
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.password = password;
    System.out.println("Set Result username="+this.username + " password="+this.password);
}

/****
 * synchronized
 * ****/
public  void getValue() {
    System.out.println("Get Value username="+username + " password="+password);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    A_003DirtRead dirtRead = new A_003DirtRead();
    //dirtRead.getValue();
    new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            dirtRead.setValue("tomcat", "jboss");
        }
    }).start();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    dirtRead.getValue();

}

}

004JAVA多线程脏读

标签:脏读   syn   rgs   oss   const   run   cto   ack   inter   

原文地址:https://blog.51cto.com/6000734/2370068

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!