Synchronized的作用:
能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全的效果
Synchronized的两个用法:
1)对象锁
包括方法锁(默认锁对象为this当前实例对象)和同步代码块锁(自己指定锁对 象)
2)类锁:
指Synchronized修饰静态的方法或指定锁为Class对象


1 public class SynchronizedObjectCodeBlock2 implements Runnable { 2 3 static SynchronizedObjectCodeBlock2 intface = new SynchronizedObjectCodeBlock2(); 4 Object lock1 = new Object(); 5 Object lock2 = new Object(); 6 7 @Override 8 public void run() { 9 synchronized (lock1) { 10 System.out.println("lock1部分。我叫:" + Thread.currentThread().getName()); 11 try { 12 Thread.sleep(3000); 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 System.out.println(Thread.currentThread().getName() + "lock1部分运行结束!"); 17 } 18 19 synchronized (lock2) { 20 System.out.println("lock2部分。我叫:" + Thread.currentThread().getName()); 21 try { 22 Thread.sleep(3000); 23 } catch (InterruptedException e) { 24 e.printStackTrace(); 25 } 26 System.out.println(Thread.currentThread().getName() + "lock2部分运行结束!"); 27 } 28 } 29 30 public static void main(String[] args) { 31 Thread t1 = new Thread(intface); 32 Thread t2 = new Thread(intface); 33 t1.start(); 34 t2.start(); 35 while (t1.isAlive() || t2.isAlive()) { 36 37 } 38 System.out.println("finshed"); 39 } 40 }
运行结果:


1 public class SynchronizedObjectMethod3 implements Runnable { 2 3 static SynchronizedObjectMethod3 intface = new SynchronizedObjectMethod3(); 4 5 @Override 6 public void run() { 7 method(); 8 } 9 10 public static void main(String[] args) { 11 Thread t1 = new Thread(intface); 12 Thread t2 = new Thread(intface); 13 t1.start(); 14 t2.start(); 15 while (t1.isAlive() || t2.isAlive()) { 16 17 } 18 System.out.println("finshed"); 19 } 20 21 public synchronized void method() { 22 System.out.println("我是对象锁的方法修饰符形式。我叫:"+ Thread.currentThread().getName()); 23 try { 24 Thread.sleep(3000); 25 } catch (InterruptedException e) { 26 e.printStackTrace(); 27 } 28 System.out.println(Thread.currentThread().getName()+"运行结束"); 29 } 30 }
运行结果:


1 public class SynchronizedClassStatic4 implements Runnable { 2 3 static SynchronizedClassStatic4 intface1 = new SynchronizedClassStatic4(); 4 static SynchronizedClassStatic4 intface2 = new SynchronizedClassStatic4(); 5 6 @Override 7 public void run() { 8 method(); 9 } 10 11 public static synchronized void method(){ 12 System.out.println("我是类锁的第一种形式:static形式。我叫:"+ Thread.currentThread().getName()); 13 try { 14 Thread.sleep(3000); 15 } catch (InterruptedException e) { 16 e.printStackTrace(); 17 } 18 System.out.println(Thread.currentThread().getName()+"运行结束"); 19 } 20 21 public static void main(String[] args) { 22 Thread t1 = new Thread(intface1); 23 Thread t2 = new Thread(intface2); 24 t1.start(); 25 t2.start(); 26 while (t1.isAlive() || t2.isAlive()) { 27 28 } 29 System.out.println("finshed"); 30 } 31 }
运行结果:


1 public class SynchronizedClassClass5 implements Runnable { 2 3 static SynchronizedClassClass5 intface1 = new SynchronizedClassClass5(); 4 static SynchronizedClassClass5 intface2 = new SynchronizedClassClass5(); 5 6 @Override 7 public void run() { 8 method(); 9 } 10 11 public void method(){ 12 synchronized (SynchronizedClassClass5.class){ 13 System.out.println("我是类锁的第2种形式:synchronized (*.class)形式。我叫:"+ Thread.currentThread().getName()); 14 try { 15 Thread.sleep(3000); 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 System.out.println(Thread.currentThread().getName()+"运行结束"); 20 } 21 } 22 23 public static void main(String[] args) { 24 Thread t1 = new Thread(intface1); 25 Thread t2 = new Thread(intface2); 26 t1.start(); 27 t2.start(); 28 while (t1.isAlive() || t2.isAlive()) { 29 30 } 31 System.out.println("finshed"); 32 } 33 }
运行结果: