synchronized可以对两种对象加锁:实例对象和类对象。下边先说对类对象加锁的代码:
第1是修饰static方法,第2种是直接锁类的class对象;
/*** @title: SynchronizedStaticDemo1* @description: synchronized 对类加锁1* @author: * @date: 2023/11/18 19:29*/
public class SynchronizedStaticDemo1 {private static int data = 0;public static void main(String[] args) {Thread thread1 = new Thread("线程1"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {increment();}}};thread1.start();Thread thread2 = new Thread("线程2"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {increment();}}};thread2.start();}public synchronized static void increment(){SynchronizedStaticDemo1.data ++;System.out.println(SynchronizedStaticDemo1.data +" "+ Thread.currentThread().getName());}
}
/*** @title: SynchronizedStaticDemo2* @description: synchronized 对类加锁2* @author: * @date: 2023/11/18 19:29*/
public class SynchronizedStaticDemo2 {private static int data = 0;public static void main(String[] args) {Thread thread1 = new Thread("线程1"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {synchronized (SynchronizedStaticDemo2.class){data ++;System.out.println(data +" "+ Thread.currentThread().getName());}}}};thread1.start();Thread thread2 = new Thread("线程2"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {synchronized (SynchronizedStaticDemo2.class){data ++;System.out.println(data +" "+ Thread.currentThread().getName());}}}};thread2.start();}
}
synchronized对某个实例对象加锁,如下三种方法:
/*** @title: SynchronizedInstanceDemo1* @description: synchronized修饰实例对象* @author: * @date: 2023/11/18 19:29*/
public class SynchronizedInstanceDemo1 {private static int data = 0;public static void main(String[] args) {SynchronizedInstanceDemo1 demo = new SynchronizedInstanceDemo1();Thread thread1 = new Thread("线程1"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {demo.increment();}}};thread1.start();Thread thread2 = new Thread("线程2"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {demo.increment();}}};thread2.start();}public synchronized void increment(){SynchronizedInstanceDemo1.data ++;System.out.println(SynchronizedInstanceDemo1.data +" "+ Thread.currentThread().getName());}
}
/*** @title: SynchronizedInstanceDemo2* @description: synchronized修饰实例对象* @author: * @date: 2023/11/18 19:29*/
public class SynchronizedInstanceDemo2 {private static int data = 0;public static void main(String[] args) {SynchronizedInstanceDemo2 demo = new SynchronizedInstanceDemo2();Thread thread1 = new Thread("线程1"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {synchronized (demo){data ++;System.out.println(data +" "+ Thread.currentThread().getName());}}}};thread1.start();Thread thread2 = new Thread("线程2"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {synchronized (demo){data ++;System.out.println(data +" "+ Thread.currentThread().getName());}}}};thread2.start();}
}
/*** @title: SynchronizedInstanceDemo3* @description: synchronized修饰实例对象* @author: * @date: 2023/11/18 19:29*/
public class SynchronizedInstanceDemo3 {private static int data = 0;public static void main(String[] args) {SynchronizedInstanceDemo3 demo = new SynchronizedInstanceDemo3();Thread thread1 = new Thread("线程1"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {demo.increment();}}};thread1.start();Thread thread2 = new Thread("线程2"){@Overridepublic void run() {for (int i = 0; i < 10; i++) {demo.increment();}}};thread2.start();}public void increment(){synchronized (this){data ++;System.out.println(data +" "+ Thread.currentThread().getName());}}
}