This series of blog is mainly practical, and it doesn't introduce the long winded things. The content mainly comes from the book "Java multithreaded programming core technology"
The keyword synchronized can also be applied to static static methods. If it is written in this way, the Class class corresponding to the current. java file will be locked.
Example 1: Class lock can work on all object instances of a Class
public class Service {
synchronized public static void printA() {
try {
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "Get into printA");
Thread.sleep(3000);
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "leave printA");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public static void printB() {
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "Get into printB");
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "leave printB");
}
}
public class ThreadA extends Thread {
private Service service;
public ThreadA(Service service) {
this.service = service;
}
@Override
public void run() {
service.printA();
}
}
public class ThreadB extends Thread {
private Service service;
public ThreadB(Service service) {
this.service = service;
}
@Override
public void run() {
service.printB();
}
}
public class Run {
public static void main(String[] args) {
Service service1 = new Service();
Service service2 = new Service();
ThreadA a = new ThreadA(service1);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service2);
b.setName("B");
b.start();
}
}
Example 2: the function of synchronized (class) code block is the same as that of synchronized static method
public class Service {
public static void printA() {
synchronized (Service.class) {
try {
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "Get into printA");
Thread.sleep(3000);
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "leave printA");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void printB() {
synchronized (Service.class) {
try {
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "Get into printB");
Thread.sleep(3000);
System.out.println("The thread name is:" + Thread.currentThread().getName() + "stay" + System.currentTimeMillis() + "leave printB");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadA extends Thread {
private Service service;
public ThreadA(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.printA();
}
}
public class ThreadB extends Thread {
private Service service;
public ThreadB(Service service) {
super();
this.service = service;
}
@Override
public void run() {
service.printB();
}
}
public class Run {
public static void main(String[] args) {
Service service1 = new Service();
Service service2 = new Service();
ThreadA a = new ThreadA(service1);
a.setName("A");
a.start();
ThreadB b = new ThreadB(service2);
b.setName("B");
b.start();
}
}