码迷,mamicode.com
首页 > 编程语言 > 详细

java synchronized

时间:2015-08-11 19:28:58      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:


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方法,同一时刻只能有一个线程得到执行,另一个线程必须等待。

java synchronized

标签:

原文地址:http://my.oschina.net/u/242764/blog/490871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!