004JAVA多线程脏读

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多线程脏读
本文网址:http://scyanting.com/article/pciopp.html