There are two kinds of lock objects: Class object and Class template object
1. Under standard conditions, two threads and one object, print and send text messages or call first? send message
2. After setting the SMS method with a delay of 4 seconds, two threads and one object print and send SMS or call first? send message
The object of the Synchronized lock is the caller of the method, so the two methods use the same lock. Who gets it first and who executes it
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test01 { public static void main(String[] args) { Phone phone = new Phone(); new Thread(()->{ phone.sendMsg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone.call(); },"B").start(); } } //Resource class class Phone{ public synchronized void sendMsg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"send message"); } public synchronized void call(){ System.out.println(Thread.currentThread().getName()+"phone"); } }
3. Two threads, one object, one synchronization method and one common method. Print and send SMS or hello first? hello
Because the method of sending text messages will be delayed by 4 seconds, hello is an ordinary method, no lock, not a synchronization method. It is not affected by the lock, and there is no preemption of resources.
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test02 { public static void main(String[] args) { Phone2 phone = new Phone2(); new Thread(()->{ phone.sendMsg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone.hello(); },"B").start(); } } //Resource class class Phone2{ public synchronized void sendMsg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"send message"); } public synchronized void call(){ System.out.println(Thread.currentThread().getName()+"phone"); } //Common method public void hello(){ System.out.println(Thread.currentThread().getName()+"=>hello"); } }
4. Two threads, two objects and two synchronization methods, print and send text messages or call first? phone
There will be two locks for two objects. The locks are different, so they are executed in chronological order
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test03 { public static void main(String[] args) { Phone3 phone1 = new Phone3(); Phone3 phone2 = new Phone3(); new Thread(()->{ phone1.sendMsg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone2.call(); },"B").start(); } } //Resource class class Phone3{ public synchronized void sendMsg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"send message"); } public synchronized void call(){ System.out.println(Thread.currentThread().getName()+"phone"); } //Common method public void hello(){ System.out.println(Thread.currentThread().getName()+"=>hello"); } }
5. Two threads, one object and two static synchronization methods, print and send text messages or call first? send message
When the synchronization method is static, its Class template is locked, and a Class has only one Class object
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test04 { public static void main(String[] args) { Phone4 phone4 = new Phone4(); new Thread(()->{ phone4.msg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone4.call(); },"B").start(); } } class Phone4{ public static synchronized void msg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message"); } public static synchronized void call(){ System.out.println("phone"); } }
6. Two threads, two objects and two static synchronization methods, print and send text messages or call first? send message
When the synchronization method is static, its Class template is locked, and a Class has only one Class object
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test04 { public static void main(String[] args) { Phone4 phone1 = new Phone4(); Phone4 phone2 = new Phone4(); new Thread(()->{ phone1.msg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone2.call(); },"B").start(); } } class Phone4{ public static synchronized void msg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message"); } public static synchronized void call(){ System.out.println("phone"); } }
7. Two threads, one object, one static synchronization method and one ordinary synchronization method. Print and send text messages or call first? phone
Static synchronization methods lock Class objects, while ordinary synchronization methods lock Class objects. Therefore, they have two locks that do not conflict with each other
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test05 { public static void main(String[] args) { Phone5 phone = new Phone5(); new Thread(()->{ phone.msg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone.call(); },"B").start(); } } class Phone5{ public static synchronized void msg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message"); } public synchronized void call(){ System.out.println("phone"); } }
8. Two threads, two objects, a static synchronization method and a common synchronization method. Print and send text messages or call first? phone
It has two locks, so it outputs the signal without delay first
package com.itheima.lock8; import java.util.concurrent.TimeUnit; public class Test05 { public static void main(String[] args) { Phone5 phone1 = new Phone5(); Phone5 phone2 = new Phone5(); new Thread(()->{ phone1.msg(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone2.call(); },"B").start(); } } class Phone5{ public static synchronized void msg(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("send message"); } public synchronized void call(){ System.out.println("phone"); } }