The phenomenon of 8 locks actually corresponds to 8 questions. After mastering these 8 questions, you can clearly determine who is locked
1. Multiple threads use the same lock
import java.util.concurrent.TimeUnit; public class lock1 { public static void main(String[] args) { Phone phone = new Phone(); new Thread(()->{ phone.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone.call(); },"B").start(); } } class Phone{ public synchronized void sendemail(){ System.out.println("sendemail"); } public synchronized void call(){ System.out.println("call"); }}Copy the code
The object of synchronized lock is the method caller (phone), and the two methods share the same lock. Whoever gets the lock first executes it
The thread executing sendemail gets the lock first, so execute first
2. Multiple threads are using the same lock, and one thread is blocking
import java.util.concurrent.TimeUnit; public class lock2 { public static void main(String[] args) { Phone2 phone = new Phone2(); new Thread(()->{ phone.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone.hello(); },"B").start(); } } class Phone2{ public synchronized void sendemail(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sendemail"); } public synchronized void call(){ System.out.println("call"); }}Copy the code
Whoever gets the lock first executes, even if blocking is set in a method
- The phone object, which is the caller of the method, can make calls and send text messages
- Now there are two people, thread A and thread B, and one of them wants to make A phone call and the other wants to send A text message
- Thread A gets the lock first and sleeps with it for four seconds
- Thread B can’t get the lock and needs to wait for thread A to finish
3. Multiple threads are locked or not
import java.util.concurrent.TimeUnit; public class lock3 { public static void main(String[] args) { Phone3 phone1 = new Phone3(); new Thread(()->{ phone1.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone1.hello(); },"B").start(); } } class Phone3{ public synchronized void sendemail(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sendemail"); } public void hello(){ System.out.println("hello"); }}Copy the code
Some threads have locks, some threads do not lock, there is no competition between the two for the same lock, the order of execution is random
However, thread A sleeps for A second, so thread B is executed first
4. Multiple threads using multiple locks
import java.util.concurrent.TimeUnit; public class lock4 { public static void main(String[] args) { Phone4 phone1 = new Phone4(); Phone4 phone2 = new Phone4(); new Thread(()->{ phone1.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ phone2.call(); },"B").start(); } } class Phone4{ public synchronized void sendemail(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sendemail"); } public synchronized void call(){ System.out.println("call"); }}Copy the code
Different callers, not the same lock is used between them, A gets the lock after sleep for A second, B first appears the execution result
Lock the class template
import java.util.concurrent.TimeUnit; public class lock5 { public static void main(String[] args) { new Thread(()->{ Phone5.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ Phone5.call(); },"B").start(); } } class Phone5{ public static synchronized void sendemail(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sendemail"); } public static synchronized void call(){ System.out.println("call"); }}Copy the code
Because the class template is locked, and this template is unique, the lock is the same no matter how many objects are locked
6. The lock method caller and the lock class template
import java.util.concurrent.TimeUnit; public class lock6 { public static void main(String[] args) { Phone6 people = new Phone6(); new Thread(()->{ Phone6.sendemail(); },"A").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ people.call(); },"B").start(); } } class Phone6{ public static synchronized void sendemail(){ try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sendemail"); } public synchronized void call(){ System.out.println("call"); }}Copy the code
The two locks are different. One locks the class template, and the other locks the method caller