1.synchronized+类成员方法:(对象锁)不一样对象互相不会排斥。spa
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(); } }
结果以下:code
Thread-0:0
对象
Thread-0:1
get
Thread-0:2
io
Thread-0:3
class
Thread-0:4
方法
Thread-1:0
总结
Thread-1:1
static
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对象或者实例对象)的锁