How to judge who the lock object is! Always know what a lock is and who it is!
Deep understanding of locks
first group
/**
*8 locks are 8 questions about locks
*1. Under standard conditions, two threads print, send SMS or call: print and send SMS first
*2. Sendsms is delayed for 4 seconds. Two threads print. Send SMS or call: print and send SMS first
*/
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
//Lock problem
new Thread(()->{
phone.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone {
//The object of the synchronized lock is the caller of the method
//The two methods are the same lock. Whoever gets it first will execute it first
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println ("send SMS");
}
public synchronized void call() {
System.out.println ("call");
}
}
Group 2
/**
*8 locks are 8 questions about locks
*3. After adding a common method, print SMS or hello first: print the common method first
*4. Two objects and two synchronization methods: print and send SMS or call first: print and call first
*/
public class Test2 {
public static void main(String[] args) {
//Two objects, two callers, two locks
Phone2 phone1 = new Phone2();
Phone2 phone2 = new Phone2();
//Lock problem
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// new Thread(()->{
// phone2.hello();
// },"B").start();
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone2 {
//The object of the synchronized lock is the caller of the method
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println ("send SMS");
}
public synchronized void call() {
System.out.println ("call");
}
//There is no lock here. It is not a synchronization method and is not affected by the lock
public void hello() {
System.out.println("hello");
}
}
Group 3
/**
*5. Add two static synchronization methods. There is only one object. Print first: send SMS
*6. For two objects, add two static synchronization methods. Print first: send text messages and compare the results of non static methods
*/
public class Test3 {
public static void main(String[] args) {
//There is only one class template for two objects, static, and the lock is class
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
//Lock problem
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//Phone3 is the only class object
class Phone3 {
//The object of the synchronized lock is the caller of the method
//Static static method
//As soon as the class is loaded, the class template is locked
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println ("send SMS");
}
public static synchronized void call() {
System.out.println ("call");
}
//There is no lock here. It is not a synchronization method and is not affected by the lock
public void hello() {
System.out.println("hello");
}
}
Group 4
/**
*7. One static synchronization method, one common synchronization method, one object, print first: call and send text messages after four seconds, because there are two locks, one is the lock of class template and the other is the lock of object
*8. One static synchronization method, one common synchronization method, two objects, print first: call, send text messages after four seconds, because there are two locks
*/
public class Test4 {
public static void main(String[] args) {
//There is only one class template for two objects, static, and the lock is class
Phone4 phone1 = new Phone4();
Phone4 phone2 = new Phone4();
//Lock problem
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//Phone3 is the only class object
class Phone4 {
//The static synchronization method locks the class template
//Distinguish between class objects and instance objects
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println ("send SMS");
}
//The caller of a common synchronization method lock
public synchronized void call() {
System.out.println ("call");
}
//There is no lock here. It is not a synchronization method and is not affected by the lock
public void hello() {
System.out.println("hello");
}
}
Summary
New this concrete object
Static class unique template
This work adoptsCC agreement, reprint must indicate the author and the link to this article