synchronized在代码中的用法

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"){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    increment();
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread("线程2"){
            @Override
            public 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"){
            @Override
            public 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"){
            @Override
            public 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"){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    demo.increment();
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread("线程2"){
            @Override
            public 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"){
            @Override
            public 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"){
            @Override
            public 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"){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    demo.increment();
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread("线程2"){
            @Override
            public 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());
        }
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值