• 技术文章 >java >java基础

    synchronized在java中的修饰

    小妮浅浅小妮浅浅2021-01-09 10:50:14原创2708

    我们说synchronized让一个线程运行时,保证其安全性。那么在其中synchronized都可以进行哪些操作呢?本篇主要讲的是synchronized的修饰用法,能够对普通方法、静态方法、代码块进行操作。下面将分别对这三种修饰情况,带来代码方面的用法展示,一起来看看synchronized的修饰用法吧。

    1.修饰普通方法

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    public class SynTest8 implements Runnable {

        public static void main(String[] args) throws InterruptedException {

            SynTest8 instance1 = new SynTest8();

            Thread thread1 = new Thread(instance1);

            thread1.start();

        }

        @Override

        public void run() {

            if(Thread.currentThread().getName().equals("Thread-0")) {

                method1();

            }else {

                method2();

            }

        }

        public synchronized  void method1() {

            try {

                System.out.println(Thread.currentThread().getName() + "进入到了同步方法1");

                Thread.sleep(2000);

                System.out.println(Thread.currentThread().getName() + "离开同步方法1");

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        public synchronized  void method2() {

            try {

                System.out.println(Thread.currentThread().getName() + "进入到了同步方法2");

                Thread.sleep(2000);

                System.out.println(Thread.currentThread().getName() + "离开同步方法2");

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }

    2.修饰静态方法

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    public class SynTest6 implements Runnable {

        public static void main(String[] args) throws InterruptedException {

            SynTest6 instance1 = new SynTest6();

            SynTest6 instance2 = new SynTest6();

            Thread thread1 = new Thread(instance1);

            Thread thread2 = new Thread(instance2);

            thread1.start();

            thread2.start();

        }

        @Override

        public void run() {

            method1();

        }

        public synchronized static void method1() {

            try {

                System.out.println(Thread.currentThread().getName() + "进入到了静态方法");

                Thread.sleep(2000);

                System.out.println(Thread.currentThread().getName() + "离开静态方法,并释放锁");

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }

    在这个例子中我们实例化了两个对象instance1和instance2,并且存放在了两个不同的线程中,我们测试一下访问同一个static同步方法你会发现。即使是实例不同,锁也会生效,也就是同一时刻只能有一个线程进去。

    3.修饰代码块

    一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    /**

     * 同步线程

     */

    class SyncThread implements Runnable {

       private static int count;

      

       public SyncThread() {

          count = 0;

       }

      

       public  void run() {

          synchronized(this) {

             for (int i = 0; i < 5; i++) {

                try {

                   System.out.println(Thread.currentThread().getName() + ":" + (count++));

                   Thread.sleep(100);

                } catch (InterruptedException e) {

                   e.printStackTrace();

                }

             }

          }

       }

      

       public int getCount() {

          return count;

       }

    }

    以上就是synchronized在java中修饰的用法,当线程中的方法和代码块需要修饰时,我们可以选择synchronized的方法进行解决。看完本篇后,就可以对代码部分进行试验了。

    专题推荐:java,synchronized修饰
    上一篇:java volatile的特性是什么 下一篇:synchronized在java中的原理分析

    相关文章推荐

    • java volatile的特性是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网