java并发编程学习之synchronize(二)

栏目: Java · 发布时间: 5年前

内容简介:1,2是对象锁,3是类锁运行的结果如下:

synchronized的应用方式

  1. 代码块:作用范围在{}中,作用对象是调用这个代码块的对象。
  2. 方法:作用范围是一个方法,作用对象是调用这个方法的对象。
  3. 静态方法:作用范围是这个静态方法,作用对象是这个类的所有对象。

1,2是对象锁,3是类锁

举例

代码块

无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();
    }
}

运行的结果如下:

java并发编程学习之synchronize(二)

等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();
    }
}

运行的结果如下:

java并发编程学习之synchronize(二)

等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();
    }
}

运行的结果如下:

java并发编程学习之synchronize(二)

等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");
    }
}

运行的结果如下:

java并发编程学习之synchronize(二)

thread1等待thread2执行完才执行,说明是类锁

类所的另外一种形式

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 {
}

运行结果如下:

java并发编程学习之synchronize(二)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

高性能Python

高性能Python

(美)戈雷利克、(英)欧日沃尔德 / 东南大学出版社 / 2015-2

你的Python代码也许运行正确,但是你需要运行得更快速。通过探讨隐藏在设计备选方案中的基础理论,戈雷利克和欧日沃尔德编著的《高性能Python》将帮助你更深入地理解Python的实现。你将了解如何定位性能瓶颈,从而显著提升高数据流量程序中的代码执行效率。 你该如何利用多核架构和集群?或者你该如何搭建一个可以自由伸缩而不会影响可靠性的系统?有经验的Python程序员将会学习到这类问题的具体解......一起来看看 《高性能Python》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试