synchronized入门

为什么使用synchronized

先看案例

public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        TaskDemo demo = new TaskDemo();
        Thread taskThread = new Thread(demo);
        Thread taskThread2 = new Thread(demo);
        taskThread.start();
        taskThread2.start();
        taskThread.join();
        taskThread2.join();
        System.out.println(demo.count);
    }

    static class TaskDemo implements Runnable {
        public int count = 0;
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                runTask();
            }
        }
        public void runTask() {
            count++;
        }
    }
}

不难看出,以上案例期望的count值为2000,但执行多次后可以发现,count会出现小于2000的情况。
这是并发编程中一个典型的原子性问题,为了解决并发编程中的各种问题,JDK提供了很多工具,synchronized就是其中之一。

synchronized的使用

作为关键字级别的锁,synchronized的加锁与解锁过程是隐式的。
格式

修饰代码块 synchronized(lockObject){} 
修饰方法   synchronized methadName(){} 

修饰代码块时需要显示指定锁对象,修饰方法时隐式指定锁对象。
方法为成员方法时,以当前对象为锁对象,方法为静态方法时,以当前类对象为锁对象。
使用synchronized对先前的案例优化后代码如下

同步代码块

public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        TaskDemo demo = new TaskDemo();
        Thread taskThread = new Thread(demo);
        Thread taskThread2 = new Thread(demo);
        taskThread.start();
        taskThread2.start();
        taskThread.join();
        taskThread2.join();
        System.out.println(demo.count);
    }

    static class TaskDemo implements Runnable {
        public int count = 0;

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                runTask();
            }
        }

        public void runTask() {
            synchronized (this) {
                count++;
            }
        }
    }
}

静态同步代码块

public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        TaskDemo demo = new TaskDemo();
        Thread taskThread = new Thread(demo);
        Thread taskThread2 = new Thread(demo);
        taskThread.start();
        taskThread2.start();
        taskThread.join();
        taskThread2.join();
        System.out.println(demo.count);
    }

    static class TaskDemo implements Runnable {
        public int count = 0;

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                runTask();
            }
        }

        public void runTask() {
            synchronized (TaskDemo.class) {
                count++;
            }
        }
    }
}

同步方法

public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        TaskDemo demo = new TaskDemo();
        Thread taskThread = new Thread(demo);
        Thread taskThread2 = new Thread(demo);
        taskThread.start();
        taskThread2.start();
        taskThread.join();
        taskThread2.join();
        System.out.println(demo.count);
    }

    static class TaskDemo implements Runnable {
        public int count = 0;

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                runTask();
            }
        }

        public synchronized void runTask() {
            count++;
        }
    }
}

修饰静态同步方法

public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        TaskDemo demo = new TaskDemo();
        Thread taskThread = new Thread(demo);
        Thread taskThread2 = new Thread(demo);
        taskThread.start();
        taskThread2.start();
        taskThread.join();
        taskThread2.join();
        System.out.println(demo.count);
    }

    static class TaskDemo implements Runnable {
        public int count = 0;

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                runTask();
            }
        }

        public static synchronized void runTask() {
            count++;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早川不爱吃香菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值