synchronized

最后都变了- 提交于 2020-01-29 04:53:57

使用方法
修饰一个方法,被修饰的方法称为同步方法,其作用范围是整个方法
修饰一个代码块,被修饰的代码块称为同步代码块,作用范围是大括号{}括起来的代码
修饰一个静态方法,作用范围是整个静态方法
修饰代码块:锁的是括号内的对象

class SynchronizedTest implements Runnable{
    private static int counter = 1;
    @Override
    public void run() {
        String startDate = Synchronized.getCurrentDate();
        synchronized (this) {
            for (int i = 0; i < 5; i++) {
                try {
                    System.out.println("线程 :" + Thread.currentThread().getName() + " 当前计数器 :" + (counter++));
                    System.out.println("开始时间 :" + startDate + " 当前时间 :" + Synchronized.getCurrentDate());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        SynchronizedTest syncThread = new SynchronizedTest();
        Thread thread1 = new Thread(syncThread, "sync-thread-1");
        Thread thread2 = new Thread(syncThread, "sync-thread-2");
        thread1.start();
        thread2.start();
    }
}

在这里插入图片描述
当两个并发线程(thread1和thread2)访问同一个对象(SynchronizedTest )中的synchronized代码时在同一时刻只能有一个线程得到执行,另一个线程受阻塞,必须等待当前线程执行完这个代码块以后才能执行该代码块。Thread1和thread2是互斥的,因为在执行synchronized代码块时会锁定当前的对象,只有执行完该代码块才能释放该对象锁,下一个线程才能执行并锁定该对象。
改动main方法

public static void main(String[] args) {
        SynchronizedTest syncThread1 = new SynchronizedTest1();
        SynchronizedTest syncThread2 = new SynchronizedTest1();
        Thread thread1 = new Thread(syncThread1, "sync-thread-1");
        Thread thread2 = new Thread(syncThread2, "sync-thread-2");
        thread1.start();
        thread2.start();
    }

在这里插入图片描述
两个线程都是新建一个对象去执行的,所以锁也是两个,所以执行方式是同时执行了。
修饰方法
接口方法时不能使用synchronized关键字;
构造方法不能使用synchronized关键字,但可以使用synchronized代码块进行同步;
synchronized关键字无法继承;
如果在父类中的某个方法使用了synchronized关键字,而在子类中覆盖了这个方法,在子类中的这个方法 默认情况下并不是同步的。
子类方法同步的解决方案
子类方法也加上synchronized 关键字
子类方法中调用父类同步的方法,例如:使用 super.xxxMethod()调用父类方法
修饰普通方法:锁的是当前实例的对象

class SynchronizedTest implements Runnable {
    private static int counter = 1;
    @Override
    public synchronized void run() {
        String startDate = Synchronized.getCurrentDate();
        for (int i = 0; i < 5; i++) {
            try {
                System.out.println("线程 :" + Thread.currentThread().getName() + " 当前计数器 :" + (counter++));
                System.out.println("开始时间 :" + startDate + " 当前时间 :" + Synchronized.getCurrentDate());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        SynchronizedTest syncThread = new SynchronizedTest();
        Thread thread1 = new Thread(syncThread, "sync-thread-1");
        Thread thread2 = new Thread(syncThread, "sync-thread-2");
        thread1.start();
        thread2.start();
    }
}

在这里插入图片描述
修饰静态方法:锁的是类的class对象

class SynchronizedTest implements Runnable {
    private static int counter = 1;
    public synchronized static void method(){
        String startDate = Synchronized.getCurrentDate();
        for (int i = 0; i < 5; i++) {
            try {
                System.out.println("线程 :" + Thread.currentThread().getName() + " 当前计数器 :" + (counter++));
                System.out.println("开始时间 :" + startDate + " 当前时间 :" + Synchronized.getCurrentDate());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void run() {
        method();
    }
    public static void main(String[] args) {
        SynchronizedTest syncThread1 = new SynchronizedTest();
        SynchronizedTest syncThread2 = new SynchronizedTest();
        Thread thread1 = new Thread(syncThread1, "sync-thread-1");
        Thread thread2 = new Thread(syncThread2, "sync-thread-2");
        thread1.start();
        thread2.start();
    }
}

在这里插入图片描述
syncThread1和syncThread2是SynchronizedTest的两个对象,但在thread1和thread2并发执行时却保持了线程同步。这是因为run中调用了静态方法method,而静态方法是属于同一类的,所以syncThread1和syncThread2相当于用了同一把锁。

注: 实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!