1

synchronized的应用方式

clipboard.png

举例

同步方法块

无this

public class SynchronizeDemo1 {
    static String syn = new String();

    static class SynClass {
        public void myRun() {
            try {
                System.out.println(Thread.currentThread().getName() + "进来了");
                synchronized (syn) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "出来了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Runnable1 implements Runnable {
        SynClass synClass;

        public Runnable1(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        SynClass synClass;

        public Runnable2(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    public static void main(String[] args) {
        SynClass synClass = new SynClass();
        Runnable1 runnable1 = new Runnable1(synClass);
        Runnable2 runnable2 = new Runnable2(synClass);
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

运行的结果如下:

clipboard.png

等thread1把代码块的执行完,释放了syn的锁,thread2才开始执行。

有this

public class SynchronizeDemo2 {
    static String syn = new String();

    static class SynClass {
        public void myRun() {
            try {
                System.out.println(Thread.currentThread().getName() + "-myRun");
                synchronized (this) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "-myRun");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void myRun2() {
            try {
                System.out.println(Thread.currentThread().getName() + "-myRun2");
                synchronized (this) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "-myRun2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Runnable1 implements Runnable {
        SynClass synClass;

        public Runnable1(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        SynClass synClass;

        public Runnable2(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun2();
        }
    }

    public static void main(String[] args) {
        SynClass synClass = new SynClass();
        Runnable1 runnable1 = new Runnable1(synClass);
        Runnable2 runnable2 = new Runnable2(synClass);
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

运行的结果如下:

clipboard.png
等thread1把代码块的执行完,释放了this的锁,thread2才开始执行。

方法

public class SynchronizeDemo3 extends Thread {
    @Override
    public void run() {
        sync();
    }

    synchronized public void sync(){
        System.out.println(Thread.currentThread().getName() + "进来了");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "出来了");
    }

    public static void main(String[] args) {
        SynchronizeDemo3 synchronizeDemo1 = new SynchronizeDemo3();
        Thread thread1 = new Thread(synchronizeDemo1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(synchronizeDemo1);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

运行的结果如下:

clipboard.png

等thread1把方法执行完,释放了的锁,thread2才开始执行。

静态方法

public class SynchronizeDemo4 {
    static class Runnable1 implements Runnable {
        @Override
        public void run() {
            SynClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        @Override
        public void run() {
            SynClass.myRun2();
        }
    }

    public static void main(String[] args) {
        Runnable1 runnable1 = new Runnable1();
        Runnable2 runnable2 = new Runnable2();
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

class SynClass {
    public synchronized static void myRun() {
        System.out.println(Thread.currentThread().getName() + "-myRun");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-myRun");
    }

    public synchronized static void myRun2() {
        System.out.println(Thread.currentThread().getName() + "-myRun2");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-myRun2");
    }
}

运行的结果如下:

clipboard.png

类锁的另外一种形式

public class SynchronizeDemo5 {
    static class Runnable1 implements Runnable {
        @Override
        public void run() {
            synchronized (SynClass2.class){
                System.out.println(Thread.currentThread().getName() + "-myRun");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-myRun");
            }
        }
    }

    static class Runnable2 implements Runnable {
        @Override
        public void run() {
            synchronized (SynClass2.class){
                System.out.println(Thread.currentThread().getName() + "-myRun2");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-myRun2");
            }
        }
    }

    public static void main(String[] args) {
        Runnable1 runnable1 = new Runnable1();
        Runnable2 runnable2 = new Runnable2();
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

class SynClass2 {
}

运行结果如下:

clipboard.png

原理

clipboard.png
clipboard.png
每个对象头都有监视器,记录着锁的信息。
A线程进来,判断是否有其他线程获取锁,如果没有,获取锁。
A线程进来(另外一个方法或者递归),判断是否有其他线程获取锁,如果有,判断是否是当前线程,如果是,允许获取锁,此时锁为偏向锁。
B线程进来,判断是否有其他线程获取锁,如果有,判断是否是当前线程,如果不是,自旋,通过CAS判断是否获取锁,此时锁为轻量级锁。
C线程进来,步骤同B,如果CAS也没获取锁,则升级到重量级锁,此线程进入同步队列等待。
当其他线程执行完,会唤醒同步队列头节点的线程


大军
847 声望183 粉丝

学而不思则罔,思而不学则殆