乖乖儿这章东西真多,对于我这个不爱看书的人,真是受不了啊,这章代码多,理论也不少,需要静心整理,不过这章也是多线程技术的重中之重,所以必须要坚持下去。come on! ----- 这里笔记基本都是一个理论一个代码示例,用代码证明理论这种抽象的东西
synchronized同步方法
线程安全
:获得的实例变量的值是经过同步处理的,不会出现脏读的现象。
非线程安全
:多个线程对同一个对象中的实例变量进行并发访问时发生,产生的后果就是“脏读”。
同步方法的一些特性
- 方法内的变量为线程安全(每一个线程操作自己的局部变量)
class HashSelfPrivateNum{
public void setNum(String name){
int num = 0; // This is important
if("a".equals(name)){
num = 100;
System.out.println("a set over");
}else{
num = 200;
System.out.println(name +" set over");
}
try{
Thread.sleep(1000);
System.out.println(name + " : num = " + num);
}catch(Exception e){
e.printStackTrace();
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.setNum("over");
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
运行结果:
over set over
a set over
over : num = 200
a : num = 100
- 实例变量是非线程安全(多个线程操作同一个实例变量)
class HashSelfPrivateNum{
private int num; // This is important
public void setNum(String name){
if("a".equals(name)){
num = 100;
System.out.println("a set over");
}else{
num = 200;
System.out.println(name +"set over");
}
try{
Thread.sleep(1000);
System.out.println(name + " : num = " + num);
}catch(Exception e){
e.printStackTrace();
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.setNum("over");
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
运行结果:
a set over
overset over
over : num = 200
a : num = 200
在这里我们可以清晰的看见数据发生了错误,明明a应该是100结果却是200.这就是多个线程访问同一个实例变量带来的隐患。
避免这种隐患的方式就是在setNum方法前加一个synchronized关键字,让setNum方法成为同步方法。
- 多个对象对应多个锁(每个对象都有自己的锁,并不是公用同一个)
先将上面的代码加上synchronized看看运行结果
class HashSelfPrivateNum{
private int num; // This is important
public synchronized void setNum(String name){
if("a".equals(name)){
num = 100;
System.out.println("a set over");
}else{
num = 200;
System.out.println(name +"set over");
}
try{
Thread.sleep(1000);
System.out.println(name + " : num = " + num);
}catch(Exception e){
e.printStackTrace();
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.setNum("over");
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
//HashSelfPrivateNum numRefTwo = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
运行结果:
a set over
a : num = 100
overset over
over : num = 200
从结果可以看出线程One先执行完setNum方法,线程Two才执行setNum。
接下来证明本小节这个理论
将第53行注释拿掉,再将55行的numRef改为numRefTwo。
运行结果:
a set over
overset over
a : num = 100
over : num = 200
我们发现线程One执行setNum方法还没执行完,线程Two就开始执行setNum方法,这里说明线程One执行setNum方法是拿的numRef对象的锁,线程Two执行setNum方法拿的是numRefTwo对象的锁,他们互不干扰。
- synchronized 方法锁的是对象
- synchronized 锁的重入(当一个线程的一个对象锁后,再次请求此对象锁是可以再次得到该对象的锁的)
- 出现异常锁自动释放
class HashSelfPrivateNum{
public synchronized void setNum(String name){
System.out.println(Thread.currentThread().getName() + " set over");
if("a".equals(name)){
Integer.parseInt("adb");
}
try{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " end");
}catch(Exception e){
e.printStackTrace();
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.setNum("over");
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
运行结果:
ThreadOne set over
Exception in thread "ThreadOne" ThreadTwo set over
java.lang.NumberFormatException: For input string: "adb"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at HashSelfPrivateNum.setNum(Demo.java:7)
at ThreadOne.run(Demo.java:29)
ThreadTwo end
我们可以清楚看见当ThreadOne抛出异常ThreadTwo立刻拿到numRef对象的锁,继续执行了下去。
- 同步不具有继承性
当我们在父类写了一个同步方法,如果在子类重写此方法还需要我们重新写上synchronized关键字。
synchronized 同步语句块
效率! 效率! 效率!
synchronized 同步语句块的一些特性
- 弥补同步方法的低效性
同步方法的局限性导致的原因是synchronized的作用域在整个方法,这将导致方法中无需同步的代码也必须要同步。
例如在同步方法里有一个非常耗时的请求操作,然而我们只需要在请求得到数据后在同步设置方法。
优化代码:
class HashSelfPrivateNum{
private int num; // This is important
public void setNum(String name){
System.out.println(Thread.currentThread().getName() + " set over");
try{
Thread.sleep(5000); // 模拟非常大的数据请求
}catch(Exception e){
e.printStackTrace();
}
synchronized(this){
this.num = 100;
System.out.println(Thread.currentThread().getName() + " end");
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.setNum("over");
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
输出结果:
ThreadOne set over
ThreadTwo set over
ThreadOne end
ThreadTwo end
- synchronized 代码块间的同步性
class HashSelfPrivateNum{
public synchronized void setNum(String name){
System.out.println(Thread.currentThread().getName() + " set over");
try{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " end");
}catch(Exception e){
e.printStackTrace();
}
}
public void print(){
synchronized(this){
try{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " print");
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class ThreadOne extends Thread{
private HashSelfPrivateNum numRef;
public ThreadOne(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadOne");
}
public void run(){
numRef.setNum("a");
}
}
class ThreadTwo extends Thread{
private HashSelfPrivateNum numRef;
public ThreadTwo(HashSelfPrivateNum numRef){
this.numRef = numRef;
this.setName("ThreadTwo");
}
public void run(){
numRef.print();
}
}
public class Demo{
public static void main(String[] args) throws Exception{
HashSelfPrivateNum numRef = new HashSelfPrivateNum();
ThreadOne one = new ThreadOne(numRef);
ThreadTwo two = new ThreadTwo(numRef);
one.start();
two.start();
}
}
输出结果:
ThreadOne set over
ThreadOne end
ThreadTwo print
- 将任意对象作为对象监听器
对于一个实例我们可以让读和写方法间进行同步,但是如果是其他无关方法呢?如果我们也让他们保持同步那该多么影响效率啊~