文章目录
- 什么是并发错误?
 - 并发错误是如何产生的?
 - 演示并发错误
 - 如何解决并发错误
 - 使用synchornized解决并发错误
 - 使用ReentrantLock解决并发错误
 
什么是并发错误?
多个线程共享操作同一个对象的时候,线程体当中连续的多行操作未必能够连续执行 很可能操作只完成了一部分,时间片突然耗尽,此时,另一个线程抢到时间片,直接拿走并访问了操作不完整的数据(操作不完整的数据,从逻辑上讲是错误数据)。
并发错误是如何产生的?
-  
根本原因: 多个线程共享操作同一份数据
 -  
直接原因: 线程体当中连续的多行语句,未必能够连续执行,很可能操作只完成了一半 时间片突然耗尽。
此时另一个线程刚好抢到时间片,直接拿走了操作不完整的数据 - 错误数据。 -  
导火索: 时间片突然耗尽
 
演示并发错误
public class TestConcurrentError{
	public static void main(String[] args){
		Student stu = new Student("张朝伟","先生");
		PrintThread pt = new PrintThread(stu);
		ChangeThread ct = new ChangeThread(stu);
		pt.start();
		ct.start();
	}
}
class PrintThread extends Thread{
	Student stu;
	public PrintThread(Student stu){
		this.stu = stu;
	}
	@Override
	public void run(){
		while(true){
			System.out.println(stu);
		}
	}
}
class ChangeThread extends Thread{
	Student stu;
	public ChangeThread(Student stu){
		this.stu = stu;
	}
	@Override
	public void run(){
		boolean isOkay = true;
		while(true){
				if(isOkay){ 
					stu.name = "张曼玉";
					stu.gender = "女士";
				}else{ 
					stu.name = "梁朝伟";
					stu.gender = "先生";
				}
				isOkay = !isOkay;
		}
	}
}
class Student{
	String name;
	String gender;//性别
	public Student(String name,String gender){
		this.name = name;
		this.gender = gender;
	}
	@Override
	public String toString(){
		return name + " : " + gender;
	}
}
 
我们看运行结果发现一个非常严重的问题
 
我们的代码从来没有写过将梁朝伟赋值为女士,也没有写过将张曼玉赋值为女士我们的程序为什么会出现这样的情况 ?
线程体当中连续的多行操作未必能够连续执行 假如我们将stu的名字赋值为梁朝伟,此时CPU时间片耗尽了,另一个打印的线程抢到时间片的情况下 就会将原来的正确的值改为错误的数据 从而产生并发错误。
如何解决并发错误
要想解决并发错误加锁是必须的
使用synchornized解决并发错误
synchronize语法级别的加锁也叫 互斥锁=互斥标记=锁标记=锁旗标=监视器= Monitor
synchornized修饰代码块
synchronized(临界资源){
	需要连续执行的操作
}
 
synchornized修饰整个方法
public synchronized void add(){
  
}
//等价于
public void add(){
	synchronized(){
	}
}
 
注意:即便synchronized加在方法上,其实还是对对象进行加锁,而且锁的是调用方法的那个对象Java世界里只有每个对象才有锁标记,所以加锁只能对对象加锁。
public class TestConcurrentError{
	public static void main(String[] args){
		Student stu = new Student("张朝伟","先生");//勋勋
		PrintThread pt = new PrintThread(stu);//A女孩
		ChangeThread ct = new ChangeThread(stu);//C女孩
		pt.start();
		ct.start();
	}
}
//1st.用于打印显示数据的线程 => 勋勋
class PrintThread extends Thread{
	Student stu;
	public PrintThread(Student stu){
		this.stu = stu;
	}
	@Override
	public void run(){
		while(true){
			synchronized(stu){//我们要对一组连续的操作加锁 不要对所有操作加锁
							  //我们去厕所 只是锁一次上厕所的过程 不要一辈子死在厕所里
				System.out.println(stu);
			}
		}
	}
}
class ChangeThread extends Thread{
	Student stu;
	public ChangeThread(Student stu){
		this.stu = stu;
	}
	@Override
	public void run(){
		boolean isOkay = true;
		while(true){
			synchronized(stu){
				if(isOkay){ //梁朝伟 先生
					stu.name = "张曼玉";//张曼玉 先生 |~
					stu.gender = "女士";//张曼玉 女士 |~
				}else{ //张曼玉 女士
					stu.name = "梁朝伟";//梁朝伟 女士 |~
					stu.gender = "先生";//梁朝伟 先生
				}
				isOkay = !isOkay;
			}
		}
	}
}
class Student{
	String name;
	String gender;//性别
	public Student(String name,String gender){
		this.name = name;
		this.gender = gender;
	}
	@Override
	public String toString(){
		return name + " : " + gender;
	}
}
 
使用ReentrantLock解决并发错误
java.util.concurrent.locks.ReentrantLock(jdk 5.0开始):java包的工具包的并发包的 可重入锁
 ReentrantLock :lock(加锁) unlock(解锁):放在finally{}中
 ReentrantLock可以在构造方法中传公平锁和非公平锁(公平与否针对第一个先来的线程而言)
 公平锁:new Reetrantlock(true);
- JDK6.0之前这个Lock的机制比synchronized效率高很多 JDK6.0开始
 - 重新对synchronized修改了底层实现,加入了一堆新的概念 (偏向锁 轻量级锁 锁的自旋机制) 从JDK6.0开始 synchronized 跟 Lock性能上不相上下
 
import java.util.concurrent.locks.*;
public class TestConcurrentErrorWithLock{
	public static void main(String[] args){
		Lock lock = new ReentrantLock();
		Student stu = new Student("张朝伟","先生");//勋勋
		PrintThread pt = new PrintThread(stu,lock);//A女孩
		ChangeThread ct = new ChangeThread(stu,lock);//C女孩
		pt.start();
		ct.start();
	}
}
//1st.用于打印显示数据的线程 => 勋勋
class PrintThread extends Thread{
	Student stu;
	Lock lock;
	public PrintThread(Student stu,Lock lock){
		this.stu = stu;
		this.lock = lock;
	}
	@Override
	public void run(){
		while(true){
			lock.lock();//锁 既是一个名词 又是一个动词
			try{
			//synchronized(stu){//我们要对一组连续的操作加锁 不要对所有操作加锁
							  //我们去厕所 只是锁一次上厕所的过程 不要一辈子死在厕所里
				System.out.println(stu);
			//}
			}finally{
				lock.unlock();
			}
		}
	}
}
class ChangeThread extends Thread{
	Student stu;
	Lock lock;
	public ChangeThread(Student stu,Lock lock){
		this.stu = stu;
		this.lock = lock;
	}
	@Override
	public void run(){
		boolean isOkay = true;
		while(true){
			//synchronized(stu){
				lock.lock();
				try{
					if(isOkay){ //梁朝伟 先生
						stu.name = "张曼玉";//张曼玉 先生 |~
						stu.gender = "女士";//张曼玉 女士 |~
					}else{ //张曼玉 女士
						stu.name = "梁朝伟";//梁朝伟 女士 |~
						stu.gender = "先生";//梁朝伟 先生
					}
					isOkay = !isOkay;
				}finally{
					lock.unlock();
				}
			//}
		}
	}
}
class Student{
	String name;
	String gender;//性别
	public Student(String name,String gender){
		this.name = name;
		this.gender = gender;
	}
	@Override
	public String toString(){
		return name + " : " + gender;
	}
}
 

此时已经解决了并发错误



















