java synchronized

本文详细解析了Java中synchronized关键字的使用方式及其在不同场景下的表现,包括对象锁、类锁以及它们在多线程环境下的互斥与并发特性。通过具体的代码示例,展示了synchronized关键字如何确保线程安全。

 

1.synchronized+类成员方法:(对象锁)不同对象互相不会排斥。

    a.同一对象调用不同synchronized方法:相同对象synchronized方法会互相排斥。

public class Test  {
		
	public synchronized void sy() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void sy2() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
		
	public static void main(String[] args) {
		final Test t1 = new Test();
		final Test t2 = new Test();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
		new Thread(new Runnable(){
			public void run() {
				t1.sy2();//t1对象
			}}).start();
	}

}

 

 

结果如下:

 

Thread-0:0

Thread-0:1

Thread-0:2

Thread-0:3

Thread-0:4

Thread-1:0

Thread-1:1

Thread-1:2

Thread-1:3

Thread-1:4

 

 

 

b.同一对象调用同一synchronized方法:相同对象会排斥。

public class Test  {
		
	public synchronized void sy() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
		
	public static void main(String[] args) {
		final Test t1 = new Test();
		final Test t2 = new Test();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
	}

}

结果如下:

Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4

 

 c.不同对象调用不同synchronized方法:不同对象不会排斥。

public class Test  {
		
	public synchronized void sy() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void sy2() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
		
	public static void main(String[] args) {
		final Test t1 = new Test();
		final Test t2 = new Test();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
		new Thread(new Runnable(){
			public void run() {
				t2.sy2();//t2对象
			}}).start();
	}

}

 

结果如下:

Thread-0:0
Thread-1:0
Thread-0:1
Thread-1:1
Thread-0:2
Thread-1:2
Thread-0:3
Thread-1:3
Thread-0:4
Thread-1:4

 

 d.不同对象调用同一synchronized方法:不同对象不会排斥。

public class Test  {
		
	public synchronized void sy() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public synchronized void sy2() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
		
	public static void main(String[] args) {
		final Test t1 = new Test();
		final Test t2 = new Test();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
		new Thread(new Runnable(){
			public void run() {
				t2.sy());//t2对象
			}}).start();
	}

}

结果如下:

Thread-0:0
Thread-1:0
Thread-1:1
Thread-0:1
Thread-0:2
Thread-1:2
Thread-0:3
Thread-1:3
Thread-1:4
Thread-0:4

 

 

 

 

2.synchronized+类静态成员方法:(类锁)所有对象会排斥。

    a.(同一/不同)对象调用(同一/不同)synchronized方法:都会排斥。

public class Test  {
		
	public static synchronized void sy() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static synchronized void sy2() {
		for(int i=0; i<5; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
		
	public static void main(String[] args) {
		final Test t1 = new Test();
		final Test t2 = new Test();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象
			}}).start();
		new Thread(new Runnable(){
			public void run() {
				t1.sy();//t1对象       a
				//t1.sy2();//t1对象    b
				//t2.sy();//t2对象     c
				//t2.sy2();//t2对象    d
			}}).start();
	}

}

a,b, c, d四种情况:

Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4

 


总结:

1.多个线程调用同一对象的不同synchronized方法,同一时刻只能有一个线程得到执行,另一个线程必须等待。

2.多个线程调用不同对象的相同synchronized方法,互不影响。

3.多个线程调用(同一/不同)对象的(同一/不同)static+synchronized方法,同一时刻只能有一个线程得到执行,另一个线程必须等待。

关键:

静态方法的锁为Class类对象,非静态方法的锁为实例对象。某一时刻,只能有一个线程持有该对象(或者Class对象或者实例对象)的锁

转载于:https://my.oschina.net/u/242764/blog/490871

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值