Synchronized关键字浅析

总结的话写在前面:

Synchronized(A){

   //代码块

 。。。。。

}

A可以表示普通成员变量,静态成员变量,类实例,类等。

持有A所在的内存区域的锁时才能执行该段代码块。

所以对于多个线程执行的同步代码块是否需要获取同一把锁,关键是看Synchronized修饰词A所指向的是否为同一个内存地址。

来看代码:

public class Count {
    int num = 200;
 
    public void count_num() {
        this.num--;
    }
 
    public static void main(String args[]) {
        Count c = new Count();
        ThreadNum t1 = new ThreadNum(c);
        ThreadNum t2 = new ThreadNum(c);
        ThreadNum t3 = new ThreadNum(c);
        ThreadNum t4 = new ThreadNum(c);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
 
class ThreadNum extends Thread {
    Count t_;
    String name;
 
    public ThreadNum(Count t) {
        this.t_ = t;
    }
 
    public void run() {
       while (true) {
            synchronized (t_) {
                this.name = Thread.currentThread().getName();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(t_.num > 0){
                    t_.count_num();
                     
                }else{
                    break;
                }
                System.out.println(this.name + " " + t_.num);               
            }
        }
    }
}

t1,t2,t3,t4四个线程的同步代码块共用的时同一个对象实例c的锁,内存中都指向同一个Count引用,所以实现了count有序递减。

再来看Synchronized修饰类成员变量:

public class ThreadTestffd {
 
 
	private Integer a = 0;
	private Integer b = 0;
	public static void main(String[] args) {
		ThreadTestffd test = new ThreadTestffd();
		test.A();
		test.B();
	}
 
 
	public void A() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("A线程准备");
 
 
				synchronized (a) {
					System.out.println("A线程开始");
					for (int i = 0; i < 10; i++) {
						System.out.println("a" + i);
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
	}
 
 
	public void B() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("B线程准备");
				synchronized (b) {
					System.out.println("B线程开始");
					for (int i = 0; i < 10; i++) {
						System.out.println("b" + i);
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
	}
}

A方法Synchronized修饰成员变量a,B方法修饰成员变量b,因为a和b都是同一个对象(Integer)且内存中指向的地址一致(test.a==test.b)为true,故最后的结果是先等a方法运行完之后,才打印出来b线程开始。

如果把代码改成

    private  Integer a = 0;
	private  Integer b = 0;
	public static void main(String[] args) {
		ThreadTestffd test = new ThreadTestffd();
		test.A();
		ThreadTestffd test2 = new ThreadTestffd();
		test2.B();
	}

同样的分析过程:

(test.a==test2.b)为true,故最后的结果是先等a方法运行完之后,才打印出来b线程开始。

如果把代码改成

    private  Integer a = 0;
	private  Integer b = 1;
	public static void main(String[] args) {
		ThreadTestffd test = new ThreadTestffd();
		test.A();
		test.B();
	}

那么,a和b内存地址指向不一致(test.a==test.b)为false,故最后的结果是a和b线程交替打印。

代码改成

	private Person a=new Person();
	private Person b=new Person();
	public static void main(String[] args) {
		ThreadTestffd test = new ThreadTestffd();
		test.A();
		test.B();
	}

那么,a和b内存地址指向两个不同的Person引用(test.a==test.b)为false,故最后的结果是a和b线程交替打印。

代码改成

    private Integer[] a = new Integer[]{0,1};
	private Integer[] b = new Integer[]{2,1};
	public static void main(String[] args) {
		ThreadTestffd test = new ThreadTestffd();
		test.A();
		test.B();
		System.out.println(test.a[1]==test.b[1]);
	}

且方法A和方法B中的Synchronized关键字修饰变量由a,b分别改为a[1],b[1],因为(test.a[1]==test.b[1])为true,所以a,b虽然是不同的数组,但在a[1]和b[1]内存中指向的地址是一致的,所以最后的结果是先等a方法运行完之后,才打印出来b线程开始。

同理,不难理解Synchronized修饰静态方法(public static synchronized void test(){})和代码块Synchronized(A.Class){

}

都是指向内存中方法区内某个类的锁,所以对于同一个类来说,他们是同一把锁

猜你喜欢

转载自blog.csdn.net/zzdgtb/article/details/83142792