下面将通过经典的8锁问题,认清锁原理
场景一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.util.concurrent.TimeUnit;
public class LockDemo1 { public static void main(String[] args) throws InterruptedException { Phone1 phone1 = new Phone1(); new Thread(()->{ phone1.sendEmail(); },"A").start(); TimeUnit.SECONDS.sleep(3); new Thread(()->{ phone1.callPhone(); },"B").start(); } } class Phone1{ public synchronized void sendEmail(){ System.out.println("senEmail"); } public synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.util.concurrent.TimeUnit;
public class LockDemo2 { public static void main(String[] args) throws InterruptedException { Phone2 phone2 = new Phone2(); new Thread(()->{ try { phone2.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start(); TimeUnit.SECONDS.sleep(2); new Thread(()->{ phone2.callPhone(); },"B").start(); } } class Phone2{ public synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); } public synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.concurrent.TimeUnit;
public class LockDemo3 { public static void main(String[] args) throws InterruptedException { Phone3 phone3 = new Phone3(); new Thread(()->{ try { phone3.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(2); new Thread(()->{ phone3.callPhone(); },"B").start(); } } class Phone3{ public synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(4); System.out.println("sendEmail"); }
public void callPhone(){ System.out.println("callPhone"); } }
|
场景四
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.util.concurrent.TimeUnit;
public class LockDemo4 { public static void main(String[] args) throws InterruptedException { Phone4 phoneA = new Phone4(); Phone4 phoneB = new Phone4();
new Thread(()->{ try { phoneA.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(1); new Thread(()->{ phoneB.callPhone(); },"B").start(); } } class Phone4{ public synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); } public synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景五
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.concurrent.TimeUnit;
public class LockDemo5 { public static void main(String[] args) throws InterruptedException { Phone5 phone5 = new Phone5(); new Thread(()->{ try { phone5.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(1); new Thread(()->{ phone5.callPhone(); },"B").start(); } } class Phone5{ public static synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); }
public static synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景六
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.concurrent.TimeUnit;
public class LockDemo6 { public static void main(String[] args) throws InterruptedException { Phone6 phone6 = new Phone6(); new Thread(()->{ try { phone6.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(1); new Thread(()->{ phone6.callPhone(); },"B").start(); } } class Phone6{ public static synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); }
public synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景七
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.util.concurrent.TimeUnit;
public class LockDemo7 { public static void main(String[] args) throws InterruptedException { Phone7 phoneA = new Phone7(); Phone7 phoneB = new Phone7();
new Thread(()->{ try { phoneA.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(1); new Thread(()->{ phoneB.callPhone(); },"B").start(); } } class Phone7{ public static synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); }
public static synchronized void callPhone(){ System.out.println("callPhone"); } }
|
场景八
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.util.concurrent.TimeUnit;
public class LockDemo8 { public static void main(String[] args) throws InterruptedException { Phone8 phoneA = new Phone8(); Phone8 phoneB = new Phone8();
new Thread(()->{ try { phoneA.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } },"A").start();
TimeUnit.SECONDS.sleep(1); new Thread(()->{ phoneB.callPhone(); },"B").start(); } } class Phone8{ public static synchronized void sendEmail() throws InterruptedException { TimeUnit.SECONDS.sleep(3); System.out.println("sendEmail"); }
public synchronized void callPhone(){ System.out.println("callPhone"); } }
|
小结:
1、new this 调用的这个对象,是一个具体的对象!
2、static class 唯一的一个模板!
在我们编写多线程程序得时候,只需要搞明白这个到底锁的是什么就不会出错了!synchronized(Demo.class)
和synchronized(this)