编程语言
首页 > 编程语言> > java关键字synchronized

java关键字synchronized

作者:互联网

  1. synchronized介绍:该关键字用于为Java对象、方法、代码块提供线程安全的操作。其属于独占式的悲观锁,同时属于可重入锁。
  2. synchronized的作用范围
  1. 代码实例
/*1.作用于成员变量和非静态方法时:锁住的是对象实例,即this对象;*/
public class SynchronizedDemo {
  public static void main(String[] args) {
    //
    final  SynchronizedDemo synchronizedDemo=new SynchronizedDemo();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo.generalMethod1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo.generalMethod2();
      }
    }).start();
  }
  public synchronized void generalMethod1(){//synchronized作用于普通的方法,锁住的是当前实例对象
    for (int i=1;i<3;i++){
      System.out.println("generalM1 execute "+i+" time");
    }
  }
  public synchronized void generalMethod2(){//synchronized作用于普通的方法,锁住的是当前实例对象
    for (int i=1;i<3;i++){
      System.out.println("generalM2 execute "+i+" time");
    }
  }

}
//输出结果
/*
generalM1 execute 1 time
generalM1 execute 2 time
generalM2 execute 1 time
generalM2 execute 2 time
*/
 /*2.作用于静态方法时:锁住的是Class对象;*/
package hcx.main.offer.Javasynchronized;

public class SynchronizedDemo {

  public static void main(String[] args) {
    //
    final SynchronizedDemo synchronizedDemo=new SynchronizedDemo();
     final SynchronizedDemo synchronizedDemo2=new SynchronizedDemo();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo.generalMethod1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo2.generalMethod2();
      }
    }).start();
  }
  public static synchronized void generalMethod1(){//synchronized作用于普通的方法,锁住的是当前实例对象
    for (int i=1;i<3;i++){
      System.out.println("generalM1 execute "+i+" time");
    }
  }
  public static  synchronized void generalMethod2(){//synchronized作用于普通的方法,锁住的是当前实例对象
    for (int i=1;i<3;i++){
      System.out.println("generalM2 execute "+i+" time");
    }
  }

}
/*
generalM1 execute 1 time
generalM1 execute 2 time
generalM2 execute 1 time
generalM2 execute 2 time
*/

/*3.作用于代码块时:锁住的是所有代码块中配置的对象;*/
public class SynchronizedDemo3 {
  String lockA="lockA";

  public static void main(String[] args) {
    //
    final SynchronizedDemo3 synchronizedDemo=new SynchronizedDemo3();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo.blockMethod1();
      }
    }).start();
    new Thread(new Runnable() {
      @Override
      public void run() {
        synchronizedDemo.blockMethod2();
      }
    }).start();
  }
  public  void blockMethod1(){//synchronized作用于代码块,锁住的是所有代码块中配置的对象
    synchronized (lockA) {
      for (int i = 1; i < 3; i++) {
        System.out.println("blockMethod1 execute " + i + " time");
      }
    }
  }
  public  void blockMethod2(){//synchronized作用于普通的方法,锁住的是所有代码块中配置的对象
    synchronized (lockA) {
      for (int i = 1; i < 3; i++) {
        System.out.println("blockMethod2 execute " + i + " time");
      }
    }
  }

}
//输出结果
/*
blockMethod1 execute 1 time
blockMethod1 execute 2 time
blockMethod2 execute 1 time
blockMethod2 execute 2 time
*/

标签:java,synchronized,void,锁住,关键字,实例,new,public
来源: https://www.cnblogs.com/hcxss/p/15933060.html