互联网并发编程之一 线程安全03线程中的脏读

当一个线程运行修改数据的方法,另一个线程启动读取数据的值,会读到未修改的前的数据

package com.jc.thread;

public class Thread04 {

	private String name = "abc";
	private String password = "123";

	public synchronized void setValue(String name, String password) {
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.name = name;
		this.password = password;
	}

	public void getValue() {
		System.out.println(this.name);
		System.out.println(this.password);
	}

	public static void main(String[] args) {

		final Thread04 thread04 = new Thread04();
		Thread t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				
				thread04.setValue("jkl", "333");
			}
		}, "t1");
		t1.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread04.getValue();
	}

}
输出结果为:

abc
123

对于对象的同步或者异步方法,我们在设计程序的时候一定要考虑整体性,不然会出现不一致的问题,很经典的错误就是脏读,当setValue()和getValue()同时加上synchronized 关键字,保证了业务的原子性。

package com.jc.thread;


public class Thread04 {


private String name = "abc";
private String password = "123";


public synchronized void setValue(String name, String password) {

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name;
this.password = password;
}


public synchronized void getValue() {
System.out.println(this.name);
System.out.println(this.password);
}


public static void main(String[] args) {


final Thread04 thread04 = new Thread04();
Thread t1 = new Thread(new Runnable() {


@Override
public void run() {

thread04.setValue("jkl", "333");
}
}, "t1");
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread04.getValue();
}


}

输出结果为:

jkl
333

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值