可以保证在同一时刻最多只有一个线程执行该段代码,以达到并发安全的效果java
/** * @Description: 对象锁之一,同步代码块锁 */ public class Demo1 implements Runnable { // 实例 static Demo1 instance = new Demo1(); // 自定义对象锁 Object lock1 = new Object(); Object lock2 = new Object(); public void run() { synchronized (lock1) { System.out.println("对象锁的同步代码块,锁lock1,线程名称:" + Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("lock1,"+Thread.currentThread().getName() + "运行结束!"); } synchronized (lock2) { System.out.println("对象锁的同步代码块,锁lock2,线程名称:" + Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("lock2,"+Thread.currentThread().getName() + "运行结束!"); } } public static void main(String[] args) { Thread t1 = new Thread(instance); Thread t2 = new Thread(instance); t1.start(); t2.start(); while (t1.isAlive() || t2.isAlive()) { } System.out.println("finished"); } }
运行结果以下:
/** * @Description: 对象锁之二,方法锁 */ public class Demo2 implements Runnable{ static Demo2 instance = new Demo2(); public void run() { method(); } public synchronized void method(){ System.out.println("对象锁的方法修饰符形式,名称:" +Thread.currentThread().getName() ); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "运行结束!"); } public static void main(String[] args){ Thread t1 = new Thread(instance); Thread t2 = new Thread(instance); t1.start(); t2.start(); while (t1.isAlive() || t2.isAlive()){ } System.out.println( "finished!" ); } }
运行结果以下:
注:1.只有一个class对象:java类可能会有不少个对象,可是只有1个class对象安全
2.本质:因此所谓类锁,不过是class对象的锁而已并发
3.用法和效果:类锁只能在同一时刻被一个对象所拥有this
/** * @Description: 类锁之一,static形式 */ public class Demo3 implements Runnable{ static Demo3 instance1 = new Demo3(); static Demo3 instance2 = new Demo3(); public void run() { method(); } public static synchronized void method(){ System.out.println("类锁,名称:" +Thread.currentThread().getName() ); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "运行结束!"); } public static void main(String[] args){ Thread t1 = new Thread(instance1); Thread t2 = new Thread(instance2); t1.start(); t2.start(); while (t1.isAlive() || t2.isAlive()){} System.out.println("finished!"); } }
运行结果以下:
/** * @Description: 类锁之二:synchronized(.*)形式 */ public class Demo4 implements Runnable { static Demo4 instance1 = new Demo4(); static Demo4 instance2 = new Demo4(); public void run() { method(); } private void method(){ synchronized (Demo4.class) { System.out.println("类锁,synchronized(.*)形式,名称:" + Thread.currentThread().getName() ); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "运行结束!"); } } public static void main(String[] args){ Thread t1 = new Thread(instance1); Thread t2 = new Thread(instance2); t1.start(); t2.start(); while (t1.isAlive() || t2.isAlive()){} System.err.println("finished!"); } }
运行结果以下: