脏读+synchronized使用

本文探讨了Java并发编程中脏读的问题及解决方案,通过实例详细讲解了synchronized关键字的应用,包括业务一致性保证、锁重入、异常处理等,并对比了对象锁、类锁和任意对象锁的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

脏读

对于对象的同步和异步的方法,在设计自己程序的时候,一定要考虑问题的整体,不然就会出现数据不一致的错误,很经典的错误就是脏读。

在我们对一个对象的方法加锁的时候,需要考虑业务的整体性,即为setValue/getValue方法同时加锁synchronized同步关键字,保证业务的原子性,不然就会出现业务错误(也从侧面保证业务的一致性)

/**
 * 业务整体需要使用完整的synchronized,保持业务的原子性。
 *
 */
public class DirtyRead {

	private String username = "hebei";
	private String password = "hb";
	
	public synchronized void setValue(String username, String password){
		this.username = username;
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		this.password = password;
		
		System.out.println("setValue最终结果:username = " + username + " , password = " + password);
	}
	
	public void getValue(){
		System.out.println("getValue方法得到:username = " + this.username + " , password = " + this.password);
	}
	
	
	public static void main(String[] args) throws Exception{
		
		final DirtyRead dr = new DirtyRead();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				dr.setValue("beijing", "bj");		
			}
		});
		t1.start();
		Thread.sleep(1000);
		
		dr.getValue();
	}
}

代码执行后,在主线程main执行的过程中,创建的子线程t1也在执行。程序的目的是:先给username、password赋值,然后获取最新的username、password的值。但是主线程在休眠1s后继续执行getValue()的时候,子线程由于内部休眠2s中,还没有完成对password的赋值,因此主线程main执行getValue()获得的password值为hb.

在Eclipse的console中输出



为了达到程序的目的,可以在getValue()添加synchronized

public synchronized void getValue()

此时,执行程序,输出结果为:



synchronized锁重入

关键字synchronized拥有锁重入的功能,也就是在使用synchronized时,当一个线程得到了一个对象的锁后,再次请求此对象时可以再次得到该对象的锁。

/**
 * synchronized的重入
 *
 */
public class SyncDubbo1 {

	public synchronized void method1(){
		System.out.println("method1..");
		method2();
	}
	public synchronized void method2(){
		System.out.println("method2..");
		method3();
	}
	public synchronized void method3(){
		System.out.println("method3..");
	}
	
	public static void main(String[] args) {
		final SyncDubbo1 sd = new SyncDubbo1();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				sd.method1();
			}
		});
		t1.start();
	}
}

在Eclipse的console中输出



/**
 * synchronized的重入
 *
 */
public class SyncDubbo2 {

	static class Main {
		public int i = 10;
		public synchronized void operationSup(){
			try {
				i--;
				System.out.println("Main print i = " + i);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	static class Sub extends Main {
		public synchronized void operationSub(){
			try {
				while(i > 0) {
					i--;
					System.out.println("Sub print i = " + i);
					Thread.sleep(100);		
					this.operationSup();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				Sub sub = new Sub();
				sub.operationSub();
			}
		});
		
		t1.start();
	}
	
	
}

上述代码中内部类Main、Main的子类Sub,在主线程main中创建子线程,子线程中创建Sub类的对象,并调用其同步方法operationSub(),在该方法中每次循环都要调用父类main中的operationSup()方法,operationSup()方法也是一个同步方法。

Eclipse的console中输出



synchronized异常

根据实际的项目需求,出现异常后,是跳过异常继续向下执行,还是结束该线程,分别进行不同的异常处理。

/**
 * synchronized异常
 *
 */
public class SyncException {

	private int i = 0;
	public synchronized void operation(){
		while(true){
			try {
				i++;
				Thread.sleep(100);
				System.out.println(Thread.currentThread().getName() + " , i = " + i);
				if(i == 10){
					Integer.parseInt("a");
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;//跳过异常,线程继续执行
				//throw new RuntimeException();//让线程停止
			}
		}
	}
	
	public static void main(String[] args) {
		
		final SyncException se = new SyncException();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				se.operation();
			}
		},"t1");
		t1.start();
	}
}
Eclipse中console输出



若代码改为

//continue;//跳过异常,线程继续执行
throw new RuntimeException();//让线程停止

run as --java application,在console中输出如下:



synchronized对象锁、类锁、任意对象锁

/**
 * 使用synchronized代码块加锁,比较灵活
 *
 */
public class ObjectLock {

	public void method1(){
		synchronized (this) {	//对象锁
			try {
				System.out.println("do method1..");
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void method2(){		//类锁
		synchronized (ObjectLock.class) {
			try {
				System.out.println("do method2..");
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private Object lock = new Object();
	public void method3(){		//任何对象锁
		synchronized (lock) {
			try {
				System.out.println("do method3..");
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) {
		
		final ObjectLock objLock = new ObjectLock();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				objLock.method1();
			}
		});
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				objLock.method2();
			}
		});
		Thread t3 = new Thread(new Runnable() {
			@Override
			public void run() {
				objLock.method3();
			}
		});
		
		t1.start();
		t2.start();
		t3.start();
	}
}

不要使用字符串常量加锁,如:synchronized ("AAA")

若对字符串变量加锁,在synchronized代码块内部不要修改加锁的这个字符串变量的值。

/**
 * 锁对象的改变问题
 *
 */
public class ChangeLock {

	private String lock = "lock";
	
	private void method(){
		synchronized (lock) {
			try {
				System.out.println("当前线程 : "  + Thread.currentThread().getName() + "开始");
				//lock = "change lock";
				Thread.sleep(2000);
				System.out.println("当前线程 : "  + Thread.currentThread().getName() + "结束");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
	
		final ChangeLock changeLock = new ChangeLock();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				changeLock.method();
			}
		},"t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				changeLock.method();
			}
		},"t2");
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t2.start();
	}
	
}
Eclipse中的console输出


若lock = "change lock";取消注释,则相当于t1在获取锁后,修改了这个lock变量的值。主线程main休眠1s后,子线程t2开始start,此时t2获取的是lock新值的锁,与t1线程运行获取的lock原值的锁不同,因此t1线程休眠2s并不会影响t2线程的正常执行。这时,run as--java application,在Eclipse的console中输出如下:



对象内部的属性发生变化,不影响对象锁

使用synchronized关键字获取的对象锁,这个对象内部的属性发生变化,不影响对象锁。

/**
 * 同一对象属性的修改不会影响锁的情况
 * @author alienware
 *
 */
public class ModifyLock {
	
	private String name ;
	private int age ;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public synchronized void changeAttributte(String name, int age) {
		try {
			System.out.println("当前线程 : "  + Thread.currentThread().getName() + " 开始");
			this.setName(name);
			this.setAge(age);
			
			System.out.println("当前线程 : "  + Thread.currentThread().getName() + " 修改对象内容为: " 
					+ this.getName() + ", " + this.getAge());
			
			Thread.sleep(2000);
			System.out.println("当前线程 : "  + Thread.currentThread().getName() + " 结束");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		final ModifyLock modifyLock = new ModifyLock();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				modifyLock.changeAttributte("张三", 20);
			}
		},"t1");
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
				modifyLock.changeAttributte("李四", 21);
			}
		},"t2");
		
		t1.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t2.start();
	}
	
}
子线程t1在执行的过程中,需要休眠2s,而主线程在休眠1s后,开始执行子线程t2。此时,在子线程t1的执行过程中对对象的name、age属性进行了修改,2s的休眠还未结束,由于changeAttributte(String name, int age)使用关键字synchronized修饰,所以需要首先获取对象锁,才能执行方法体中的内容。这时,t2等待t1执行完毕后,再获取对象锁并执行方法changeAttributte(String name, int age)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值